-1

To retrieve cookies from browsert, I wrote a simple function dispcookies(). When I compile, I got nullpointerException and then I just copied that code from dispcookies() and put in processRequest() that time I didn't get the error why it happens.

HttpServletRequest request;
HttpServletResponse response;
PrintWriter out=null;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {        
    response.setContentType("text/html;charset=UTF-8");
    try {
        out = response.getWriter();
        String name=request.getParameter("name");
        String phone=request.getParameter("phone");    
        Cookie cname=new Cookie("name",name);
        Cookie cphone=new Cookie("phone",phone);            
        response.addCookie(cname);
        response.addCookie(cphone);
        out.println("here ok");
        dispcookies ();  
    }
    catch(Exception E)
    {
        out.println(E);
    }
}

void dispcookies () throws IOException
{
    Cookie[] cookies;
    Cookie cookie;
    cookies=request.getCookies();
    if(cookies!=null)
    {
        for(int i=0;i<cookies.length;i++)
        {
            cookie=cookies[i];
        out.println("cookie name"+cookie.getName());
        out.println("cookie name"+cookie.getValue());
        }
    }
    else
    {
    out.println("no foumd cooke ");
    }    
}
Brian
  • 11,132
  • 7
  • 32
  • 43
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – CubeJockey Dec 14 '15 at 16:09
  • if you post the stack trace we can tell you what is the problem – Gaali Prabhakar Dec 14 '15 at 16:15

1 Answers1

0

At void dispcookies () you are trying to access to a property of the attribute request. WARNING: This request IS NOT THE SAME that the input parameter of the processRequest method. dispcookies doesn't have visibility over these input parameter, only can see the attribute HttpServletRequest defined at the beginning of the class and this attribute has not been initialized so every access to it will cause a null pointer exception.

If you want to access to the processRequest' request input parameter you must change the dispcookies signature to accept this object as input parameter and use it.

By the way:

cookies=request.getCookies();
if(cookies!=null) { 
    for(int i=0;i ...

could cause a null pointer exception because Cookies[] can be not null but empty and you are accessing to its zero position without checking the emptyness of the array.

Brian
  • 11,132
  • 7
  • 32
  • 43
Facepalmed
  • 683
  • 11
  • 14
  • so what should I do exactly guys I am new in this help me plz. – yogesh raut Dec 22 '15 at 12:10
  • 1) Remove useless HttpServletRequest request attribute that you declared at the first line 2) Modify dispcookies method to accept a HttpServletRequest input parameter 3) Invoke dispcookies method using HttpServletRequest request that comes as processRequest method input parameter (this is the ONLY request you need) 4) Try it, enjoy it and vote me ;) – Facepalmed Dec 22 '15 at 12:37