getcookies()null指针异常

时间:2016-03-26 16:21:40

标签: java session servlets cookies

我写了这个代码来检查并创建一个cookie。它循环遍历请求对象返回的数组。但它抛出异常。它在for循环的开头抛出。

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CookieLogin extends HttpServlet {
    PrintWriter out;    
    boolean flag;
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException{
                Cookie[] cookies = req.getCookies();
                out = res.getWriter();              
                for(int i=0;i<cookies.length;i++){
                    if(cookies[i].getName().equals("session_started")){
                        flag=true;
                        out.println("Your session started on "+cookies[i].getValue());
                    }
                }
                if(!flag){
                    String time = new java.util.Date().toString();
                    res.addCookie(new Cookie("session_started",time));
                    out.println("just started");
                }
    }
}

这是来自eclipse的Stack Trace。

java.lang.NullPointerException
    at CookieLogin.doPost(CookieLogin.java:11)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:2)

req.getCookies()返回null,因此cookies.length会抛出NullPointerException,

你应该检查req.getCookies();null;

答案 1 :(得分:1)

NullPointerException是因为如果没有cookie,getCookies()方法将返回null。因此它会抛出NullPointerException。

解决方案:检查 null

for(int i=0;cookies!=null&&i<cookies.length;i++)