我正在调用方法时出现NullpointerException

时间:2014-03-30 14:17:28

标签: java jsp servlets nullpointerexception entity

我创建了一个从数据库中选择所有玩家的实体:

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

然后我创建了一个无状态bean及其实现该方法的接口。

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

并在界面中签名

public List<Player> findAllPlayers();

然后我创建了一个无状态外观,作为控制器(MVC结构)。

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

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

为了更清楚,这就是我所做的:

  • 实体
  • 本地java bean(无状态)+接口。
  • 以及一个引用bean的Facade + Interface。

现在我已经创建了一个servlet,JSP将用它来调用FacadeLocal类(接口)。

在我的doPost方法中,我实现了这个:

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




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

}

然后我有JSP文件:

<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()%>">

但是我得到了一个nullPointerException

错误消息

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]

好的。我正在编辑我的问题。 我成功地通过执行以下操作显示了servlet中数据库的所有数据:

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>");
}
}

现在我想知道如何使用doPost方法将列表传递给JSP?

1 个答案:

答案 0 :(得分:0)

您需要检查facade是否为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()+"' />");
 }
 %>

如果这样做无法解决问题,请在可能为空的变量上使用点运算符执行类似的操作。