您好,欢迎访问本站博客!登录后台查看权限
  • 转载文章需要注意是否有转载标识,未经允许谢绝转载
  • 发贴请联系站长提权

Servlet使用时遭遇request和response相关的空指针报错

java adminXiongmao 2019-09-03 32 次浏览 0个评论
网站分享代码

错误信息:

 org.apache.catalina.core.StandardWrapperValve invoke

严重: Servlet.service() for servlet [LoginServlet] in context with path [/HttpServlet] threw exception

java.lang.NullPointerException

at servlet包.LoginServlet.doGet(LoginServlet.java:26)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:528)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)

at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.base/java.lang.Thread.run(Thread.java:835)



因为在java中,null类型不属于String类型,不能使用equals方法,所以会产生空指针的报错。而我们的HttpServletRequest或ServletRequest接收的是用户传来的数据,因此,直接运行java文件时,req的值是null,所以会遇到空指针报错。事实上不算问题,可以正常运行jsp即可。


还有一种情况是ServletResponse相关的,如果将getServletConfig().getInitParameter("username")写成

getServletConfig().getServletContext().getInitParameter("username")同样是会报空指针报错的。


如果以上都不是,考虑检查一下xml信息是否书写正确。如果正确考虑寻找其他解决方案。

已有 32 位网友参与,快来吐槽:

发表评论

分享:

支付宝

微信

站点统计