Servet-为什么我会得到空指针异常?

时间:2018-10-17 04:21:56

标签: java servlets nullpointerexception

创建一个使用cookie来存储用户访问次数的servlet?-这是我要解决的问题

这是我的html

<html>
    <head>

    </head>
     <body>
        <form action="NewServlet">
           <input type="text" name="text1">
           <input type="submit"/>

        </form>
    </body>
</html>

这是NewServlet.java

public class NewServlet extends HttpServlet {

   public void service(HttpServletRequest req,HttpServletResponse res)throws 
IOException,ServletException
   {
       res.setContentType("text/html");

   Cookie[] cook=req.getCookies();

   String s=req.getParameter("text1");
   int a=0;
   if(cook.length>0)
   {       
   for(Cookie cookie :cook)
   {
       if(cookie.getName().equals(s))
       {
           int b=Integer.parseInt(cookie.getValue());
           b=b+1;
           cookie.setValue(String.valueOf(b));
           a++;

       }
   }
   }

   if(a>0)
   {
       res.sendRedirect("NewServlet1");
   }
   else
   {
       Cookie cok=new Cookie(s,"1");
       res.addCookie(cok);
       req.setAttribute("user", s);
       RequestDispatcher r=req.getRequestDispatcher("NewServlet1");
       r.forward(req,res);
   }       
   }

}

这是NewServlet1.java

public class NewServlet1 extends HttpServlet {

protected void service(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    res.setContentType("text/html;charset=UTF-8");

   Cookie[] cookie=req.getCookies();
   String s=String.valueOf(req.getAttribute("user"));
   for(Cookie cok:cookie)
   {
       if(cok.getName().equals(s))
       {
           int a=Integer.parseInt(cok.getValue());
           res.getWriter().println("number of times visited"+a);
       }
   }


}
}

我正在做的是检查cookie是否存在,如果不存在,则在NewServlet.java上创建cookie。但是我得到了一个N​​ull-Pointer异常(错误500)。 点击提交按钮

stacktrace

at First.NewServlet.service(NewServlet.java:28)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:1)

Country Year Total 0 Germany 2010 NaN 1 France 2010 NaN 2 Italy 2010 NaN 更改为if(cook.length>0)。这样,如果if(cook !=null && cook.length > 0)为null,那么它将不会检查cook,因此避免了.length