Bom eu li os outros tópicos mas não achei a solução.
meu ListaEmpresaServlet
Banco banco = new Banco();
List<Empresa> lista = banco.getEmpresas();
request.setAttribute("empresa", lista);
RequestDispatcher rd = request.getRequestDispatcher("/listaempresa.jsp");
rd.forward(request, response);
meu "listaempresa.jsp"
<ul>
<%
List<Empresa> lista = (List<Empresa>) request.getAttribute("empresa");
for (Empresa empresa: lista) {
%>
<li><%= empresa.getNome() %></li>
<% } %>
</ul>
Error gerado
Type Exception Report
Message An exception occurred processing [listaempresa.jsp] at line [17]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: An exception occurred processing [listaempresa.jsp] at line [17]
14: <ul>
15: <%
16: List<Empresa> lista = (List<Empresa>) request.getAttribute("empresa");
17: for (Empresa empresa: lista) {
18: %>
19: <li><%= empresa.getNome() %></li>
20: <% } %>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:610)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:499)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:774)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because "lista" is null
org.apache.jsp.listaempresa_jsp._jspService(listaempresa_jsp.java:136)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:774)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:774)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.