why am I receiving NullPointerException on my jsp

2019-04-15 06:54发布

问题:

Once I click on a anchor tag on my jsp page, it perfectly works but the following exception will be thrown in console, based on this answer, I removed the jap-api*.jar files from my dependencies folder but the application still throws the exception.

   WARNING:   Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at org.apache.jsp.products.ProductShow_jsp._jspx_meth_c_if_1(ProductShow_jsp.java:211)
    at org.apache.jsp.products.ProductShow_jsp._jspService(ProductShow_jsp.java:119)
    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.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:695)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:626)
    at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:977)
    at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:701)
    at org.apache.tiles.request.jsp.JspRequest.doInclude(JspRequest.java:123)
    at org.apache.tiles.request.AbstractViewRequest.dispatch(AbstractViewRequest.java:47)
    at org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:45)
    at org.apache.tiles.request.render.ChainedDelegateRenderer.render(ChainedDelegateRenderer.java:68)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
    at org.apache.tiles.TilesContainerWrapper.render(TilesContainerWrapper.java:108)
    at org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:188)
    at org.apache.tiles.template.InsertAttributeModel.execute(InsertAttributeModel.java:132)
    at org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:299)
    at org.apache.jsp.baseLayout_jsp._jspx_meth_tiles_insertAttribute_3(baseLayout_jsp.java:166)
    at org.apache.jsp.baseLayout_jsp._jspService(baseLayout_jsp.java:93)
    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.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:575)
    at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:546)
    at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
    at org.apache.tiles.request.servlet.ServletRequest.forward(ServletRequest.java:265)
    at org.apache.tiles.request.servlet.ServletRequest.doForward(ServletRequest.java:228)
    at org.apache.tiles.request.AbstractClientRequest.dispatch(AbstractClientRequest.java:57)
    at org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:45)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:397)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:238)
    at org.apache.tiles.TilesContainerWrapper.render(TilesContainerWrapper.java:103)
    at org.apache.tiles.impl.mgmt.CachingTilesContainer.render(CachingTilesContainer.java:126)
    .....
WARNING:   Servlet.service() for servlet jsp threw exception
java.io.IOException: JSPException including path '/products/Product.jsp'.
    at org.apache.tiles.request.servlet.ServletUtil.wrapServletException(ServletUtil.java:61)
    at org.apache.tiles.request.jsp.JspRequest.doInclude(JspRequest.java:125)
    at org.apache.tiles.request.AbstractViewRequest.dispatch(AbstractViewRequest.java:47)
    at org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:45)

Code

<div id="product">
    <table>
        <tbody>
            <tr><td>ID:${product.id}</td></tr>
            .....
        </tbody>
    </table>


    <sec:authorize access="hasAnyRole('ROLE_ADMIN')">
        <c:if test="${product.available}">
            <s:if test="%{availability > 1}">
                <a href="" onclick="remove(${product.ID})">Remove</a>
            </s:if>
            <s:else>
                <c:if test="${cookie.containsKey('id')}">
                    <a href="" onclick="add(${product.ID})">Add</a>
                </c:if>
                <c:if test="${!cookie.containsKey('id')}">
                    <a href="index.jsp">index</a>
                </c:if>

            </s:else>
       </c:if>


    </sec:authorize>

</div>

Remove and Add are associated to their own javascript functions, both functions perfectly work but the exceptions will be thrown and after returning from javascript functions the page shows correct results.

回答1:

You can actually figure this out yourself with a little work. JSPs are compiled into servlets and the source code can be found under your web server's works folder. Generally, find the 'work' folder and then drill down thru the packages, which for Tomcat is usually org.apache.... (sorry can't remember the exact and this machine doesn't have Tomcat installed). Just drill down and you should find ProductShow_jsp.java source code. Goto the line number specified and you can generally figure out the reason for the exception.



回答2:

Please check if the below suggestions gets rid of the error:

a) Check if product.id or product.ID is defined

b) Check if the attribute name is correct for product.available

c) Add the closing tag for <c:if test="${product.available}">