Está dando este erro para mim, fala que o problema é no for do JSP:
package br.com.alura.gerenciador.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ListaEmpresasServlet
*/
@WebServlet("/listaEmpresas")
public class ListaEmpresasServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Banco banco = new Banco();
List<Empresa> lista = banco.getEmpresas();
request.setAttribute("empresas", lista);
RequestDispatcher rd = request.getRequestDispatcher("/listaEmpresas.jsp");
rd.forward(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.List, br.com.alura.gerenciador.servlet.Empresa"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Lista de empresas: <br />
<ul>
<%
List<Empresa> lista = (List<Empresa>)request.getAttribute("empresas");
for (Empresa empresa : lista) {
%>
<li><%= empresa.getNome() %></li>
<%
}
%>
</ul>
</body>
</html>
Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:626) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:515) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) Root Cause
java.lang.NullPointerException org.apache.jsp.listaEmpresas_jsp.jspService(listaEmpresasjsp.java:134) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) 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