Java 为什么我的 jsp 文件会收到 NullPointerException 异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22086406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why am I receiving NullPointerException on my jsp file?
提问by AlexCartio1
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.
一旦我点击了我的 jsp 页面上的锚标记,它就可以正常工作,但控制台中将抛出以下异常,根据这个答案,我jap-api*.jar
从我的依赖项文件夹中删除了文件,但应用程序仍然抛出异常。
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.
Remove 和 Add 与它们自己的 javascript 函数相关联,这两个函数都可以正常工作,但会抛出异常,从 javascript 函数返回后,页面显示正确的结果。
回答by raVan
Please check if the below suggestions gets rid of the error:
请检查以下建议是否消除了错误:
a) Check if product.id or product.ID is defined
a) 检查是否定义了 product.id 或 product.ID
b) Check if the attribute name is correct for product.available
b) 检查 product.available 的属性名称是否正确
c) Add the closing tag for <c:if test="${product.available}">
c) 添加结束标记 <c:if test="${product.available}">
回答by mikemil
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.
你实际上可以通过一点工作自己解决这个问题。JSP 被编译成 servlet,源代码可以在您的 Web 服务器的works 文件夹下找到。通常,找到'work'文件夹,然后通过包向下钻取,对于Tomcat通常是org.apache....(抱歉不记得确切的并且这台机器没有安装Tomcat)。只需向下钻取,您就会找到 ProductShow_jsp.java 源代码。转到指定的行号,您通常可以找出异常的原因。