简体   繁体   中英

Exception org.apache.jasper.JasperException: java.lang.NullPointerException

I'm trying to retrieve an attribute from servlet(viewcurrentstock.java) to jsp(viewstock.jsp). here an exception is promted saying "org.apache.jasper.JasperException: java.lang.NullPointerException"

在此处输入图片说明

viewcurrentstock.java

   ItemDBController itemDBController=new ItemDBController();
        try {
            List<Item> allItems = itemDBController.getAllItems();
            request.setAttribute("items",allItems);
            RequestDispatcher dispatcher=request.getRequestDispatcher("viewstock.jsp");
            dispatcher.forward(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ViewCurrentStock.class.getName()).log(Level.SEVERE, null, ex);
        }

viewstock.jsp

<%ArrayList<Item> itemlist = (ArrayList<Item>) request.getAttribute("items"); %>
 <html>
  <head>
      </head> 
         <body>
       <table border="2">
         <tbody>
            <%for (Item item : itemlist) {%>
            <tr>
                  <td><%=item.getItemcode()%></td>
                  <td><%=item.getItemname()%></td>
                  <td><%=item.getDescription()%></td>
                   <td><%=item.getQtyOnHand()%> </td>
                   <td> <%= item.getReorderlevel()%></td>
                   <td><%=item.getUnitprice()%></td>
            </tr>
            <%}%>
        </tbody>

    </table>

</body>
</html>

server log:

java.lang.NullPointerException
at org.apache.jsp.viewstock_jsp._jspService(viewstock_jsp.java:66)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)

Using scriplets is discouraged see here How to avoid Java code in JSP files? , print it out using jstl forEach

Add this for taglib support for jstl,

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 

And simply,

<c:forEach var="temp" items="${items}">
<tr>
                 <td>${temp.itemcode}</td>
                  <td>${temp.get.name}</td>
                  <td>${temp.description}</td>
                   <td>${temp.qtyOnHand }</td>
                   <td>${temp.reorderlevel}</td>
                   <td>${temp.unitprice}</td>
</tr>
</c:forEach>

I am not sure with variables of your bean class . the syntax is ${var.AttributeName} in the model class

Hope this helps !!

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