java Servlet.service() 抛出 NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10677335/
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
Servlet.service() throwing NullPointerException
提问by user906153
I'm honestly baffled by why this is throwing this particular exception. If I have the servlet direct to another JSP, it works fine. Here is the stack trace of the error I am getting
老实说,我对为什么会抛出这个特殊异常感到困惑。如果我有 servlet 直接到另一个 JSP,它工作正常。这是我收到的错误的堆栈跟踪
17:13:51,190 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/ProjectServlet].[jsp]] (http-localhost-127.0.0.1-8080-2) Servlet.service() for servlet jsp threw exception: java.lang.NullPointerException
at org.apache.jsp.createProfile_jsp._jspService(createProfile_jsp.java:74)
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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:840) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:622) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:560) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:488) [jbossweb-7.0.13.Final.jar:]
at ecollaborator.Servlet.CreateAccountServlet.gotoPage(CreateAccountServlet.java:55) [classes:]
at ecollaborator.Servlet.CreateAccountServlet.doPost(CreateAccountServlet.java:45) [classes:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
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(Unknown Source) [rt.jar:1.7.0]
Right now, I have a page where a user is prompted to create an account, the JSP looks like this. It is called createAccount.jsp
现在,我有一个页面提示用户创建一个帐户,JSP 看起来像这样。它被称为 createAccount.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<% String type = (String) request.getParameter("type");%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<b><font size="6"><center>E-Collaborator</center></font></b>
</head>
<body>
<form name='createAccount' action="CreateAccountServlet" method="post">
<table>
<tr>
<td>Create Account<td>
</tr>
<tr>
<td>Username:</td><td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password:</td><td><input type="password" name="password" /></td>
</tr>
<tr>
<td>Type:</td>
<td>
<select id="acctype" name="acctype">
<option></option>
<option>Collaborator</option>
<option>Consumer</option>
<option>ServiceProvider</option>
</select>
</td>
</tr>
</table>
<input type="submit" value="Submit"/>
<input type = "hidden" name="createAccount" value="true">
</form>
</body>
</html>
It is forwarded to this Servlet, where the error is being thrown in the gotoPage method, on the dispatcher.forward command.
它被转发到这个 Servlet,在 dispatcher.forward 命令的 gotoPage 方法中抛出错误。
package ecollaborator.Servlet;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ecollaborator.Entity.*;
import ecollaborator.Session.*;
@WebServlet("/CreateAccountServlet")
public class CreateAccountServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
private Admin admin;
public CreateAccountServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
boolean isForwarded = false;
String createAccount = request.getParameter("createAccount");
if(createAccount != null && createAccount.equals("true"))
{
String username = (String)request.getParameter("username");
String password = (String)request.getParameter("password");
String type = (String)request.getParameter("acctype");
User u = new User(username,password,type);
admin.createAccount(u);
if(!isForwarded)
{
request.setAttribute("type", type);
isForwarded = gotoPage("/createProfile.jsp", request, response);
}
}
}
private boolean gotoPage(String address, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
dispatcher.forward(request, response);
return true;
}
}
How can I fix this code, so that I can avoid coming across this error? My other servlets use the same gotoPage function and forward to JSPs just fine.
如何修复此代码,以避免遇到此错误?我的其他 servlet 使用相同的 gotoPage 函数并转发到 JSP 就好了。
回答by Nurlan
try to replace getParameterto getAttributein jsp
尝试在jsp中将getParameter替换为getAttribute
<% String type = (String) request.getAttribute("type");%>
and also remove line:
并删除行:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
and check
并检查
getParameter and getAttribute are different methods
getParameter 和 getAttribute 是不同的方法
回答by Vinod
On viewpage.jsp jst check The Iterator reference itr has been assinged with ArrayList object with iterator method or not As like itr = al.iterator();
在 viewpage.jsp jst 上检查 Iterator 引用 itr 是否已通过迭代器方法与 ArrayList 对象关联 就像 itr = al.iterator();