简体   繁体   中英

NullpointerException when I'm invoking a method

I have created an entity that selects all players from a database:

@NamedQueries({
@NamedQuery(name ="player.findAll",
        query = "select p from Player p")
})

and then I have created a stateless bean and its interface that implements the method.

private EntityManager em;
...
public List<Player> findAllPlayers(){
    TypedQuery <Player> query = 
            em.createNamedQuery("player.findAll", Player.class);
    List<Player> results = query.getResultList();
    return results;
}

and in interface its signature

public List<Player> findAllPlayers();

then I have created a stateless facade that works as a controller(MVC-structure).

@EJB
PlayerEAOLocal playerEAO;
...//Some more code
public List <Player> findAllPlayers(){

    return playerEAO.findAllPlayers();
}
//And in interface...
public List <Player> findAllPlayers();

To make it clearer this is what I have done:

  • An entity
  • A local java bean(stateless) + Interface.
  • and a facade + Interface that refers to the beans.

Now I have created a servlet that the JSP will use for calling the FacadeLocal class(interface).

In my doPost method I have this implemented:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);




    FacadeLocal f = (FacadeLocal) facade.findAllPlayers();
    request.setAttribute("facade", f);

}

and then I have the JSP file:

<form action = "GetAllPlayersServlet" method = "post">
<input type = "submit" name = "submit" value = "Show all"></input></td>
                <td style = "width: 2px; "></td>

                <% FacadeLocal facade = (FacadeLocal)request.getAttribute("facade"); %>
            <td><input type = "text" name= "showAll" value = "<%= facade.findAllPlayers()%>">

But I get a nullPointerException

The error message

ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/PlayerClientProject].[jsp]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet jsp threw exception: java.lang.NullPointerException
at org.apache.jsp.ShowAllPlayers_jsp._jspService(ShowAllPlayers_jsp.java:81)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [jbossweb-7.0.13.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) [jbossweb-7.0.13.Final.jar:]
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326) [jbossweb-7.0.13.Final.jar:]
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253) [jbossweb-7.0.13.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45]

Alright. I am editing my question a little bit. I have succeeded with showing all data from database in the servlet by doing:

protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
    List<Player> allPlayers = facade.findAllPlayers();

    for (Player p1 : allPlayers) {

        out.println("<h4>Founded: " + p1.getClass().getSimpleName());
        out.println(" Id: " + p1.getId());
        out.println(" - " + p1.getNickname());
        out.println(" - " + p1.getLocation());
        out.println(" - " + p1.getChampion() + "</h4>");
}
}

Now I wonder how I can pass the list to the JSP using doPost method?

You need to check facade for null:

 <%
 FacadeLocal facade = (FacadeLocal)request.getAttribute("facade"); 
 if(facade == null)
 {
   out.print("<td><input type='text' name='showAll' value='' />");
 }
 else
 {
   out.print("<td><input type='text' name='showAll' value='"+facade.findAllPlayers()+"' />");
 }
 %>

If that doesn't fix it, do something similar everwhere you are using the dot operator on a variable that may be null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM