estou com o mesmo problema do amigo abaixo, não consigo usar a minha lista de empresas no jsp, ela sempre devolme null
listaEmpresa.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%@page import="br.com.caelum.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>
ListaEmpresaServlet:
package br.com.caelum.gerenciador.servlet;
import java.io.IOException;
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;
@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.getEmpresa();
request.setAttribute("empresas", lista);
RequestDispatcher rd = request.getRequestDispatcher("/listaEmpresa.jsp");
rd.forward(request, response);
}
}
erro:
HTTP Status 500 – Internal Server Error
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("empresas");
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:379)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:327)
javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
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:138)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:379)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:327)
javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note A pilha de erros completa da causa principal está disponível nos logs do servidor.