2
respostas

Error bad Server 500

ype Exception Report

Message An exception occurred processing [listaEmpresa.jsp] at line [16]

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 [16]

13:

  • 14: <% 15: List lista = (List)request.getAttribute("empresas"); 16: for (Empresa empresa : lista) { 17: %> 18:
  • 19: <%= empresa.getNome() %>

    Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:611) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:500) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:379) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:327) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:770) 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(listaEmpresajsp.java:136) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:770) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:467) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:379) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:327) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:770) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

2 respostas
java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because "lista" is null

Por algum motivo sua lista está chegando null

Envia seu código aqui, por favor.

package br.com.alura.gerenciador.servlet;

import java.io.IOException; import java.util.List;

import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest;

/**

  • Servlet implementation class listaEmpresas */ @WebServlet(urlPatterns="/listaEmpresas") public class listaEmpresas extends HttpServlet { private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) 
        throws ServletException, IOException {
    Banco banco = new Banco();
    List<Empresa> lista = banco.getEmpresas();



    // Chamando o JSP no lugar da lista
    RequestDispatcher rd = request.getRequestDispatcher("/listaEmpresas.jsp");
    request.setAttribute("empresas", lista);
    rd.forward(request, response);



}

}