0

I'm new for what about j2EE and I'm moving my first steps with servlet and EJB. I use Netbeans, Glassfish. When I pass my data (I want to view in my JSP the data in the table Products on postgress and the connection I sure that works) from servlet to JSP I have a strange Null Pointer Exception. This is the processRequestMethod in the Servlet class:

        protected void processRequest(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException {

         List<Products> list=new ArrayList<Products>();
        list =  productsFacade.findAll(); 

        request.setAttribute("list", list);

        String arg = "/" + this.getServletName() + ".jsp";
        RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(arg);
        dispatcher.forward(request, response);
}

the method findAll():

public List<Products> findAll()
    {
      return em.createQuery("SELECT p FROM Products p").getResultList();
    }

Instead the JSP is this:

<%@page import="java.util.ArrayList"%>
<%@page import="entities.Products"%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="entities.Products_"%>
 <%@page import="java.util.List"%>
<%
    List<Products> list=new ArrayList<Products>();
    list = (List<Products>)request.getAttribute("list");
 %>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
 <html>
     <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <title>JSP Page</title>
      </head>
     <body>
     <h1>Content of my Table</h1>
     <% 

         if (list != null) 
          { for (Products e: list)
              { out.println(e.getName() + " " + e.getPrice() + "<br/>"); }
          }           
       %>    
      </body>
  </html>

Why my list is NULL ?

user2837896
  • 231
  • 3
  • 11

0 Answers0