在我的Java Servlet的init方法中,我正在调用:
public void init (ServletConfig config) throws ServletException {
ServletContext context = this.getServletContext();
}
并立即投掷:
java.lang.NullPointerException
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:123)
myproject.servlets.Login.init(Login.java:37)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)
答案 0 :(得分:2)
您已覆盖Servlet类中的init(ServletConfig config)
方法,并且未调用super.init(config)
来执行标准设置操作,从而导致空上下文。
public void init (ServletConfig config) throws ServletException {
super.init(config);
ServletContext context = this.getServletContext();
}