Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New to cookies

 
varkala prabhakar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai all,
In my servlet Program i use cookies and i am setting them using

Cookie cook = new Cookie(id,login);
response.addCookie(cook);
its working fine and when i retrive cookies in the other program using
Cookie []cook = request.getCookies();
i get the cook.length Zero and my servlet is not working and i get a null point exception.saying
java.lang.NullPointerException
at ShowCategory.doGet(ShowCategory.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:499)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:588)
at sun.servlet.http.HttpServerHandler.sendResponse(HttpServerHandler.java:165)
at sun.servlet.http.HttpServerHandler.handleConnection(HttpServerHandler.java:121)
at sun.servlet.http.HttpServerHandler.run(HttpServerHandler.java:90)
at java.lang.Thread.run(Unknown Source)

Could anyone help me how to retrive my cookie values.
thanx
prabhakar
 
Marcos Maia
Ranch Hand
Posts: 977
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check if your browser is not configured to don�t accept Cookies, you should also notice that by default a cookie only persists until browser shutdown, you can use the method setMaxAge(int expiry) wich sets the maximum age of the cookie in seconds, to change this behavior.
Also take a look at http://java.sun.com/docs/books/tutorial/servlets/client-state/cookies.html there you�ll find more information on Cookies.
regards.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The NullPointerException is because your getCookies() method will return null if there are no cookies.

When you then attempt to get the length of the cook array, 'cook' has been initialized with the null returned by getCookies() and so you throw a NullPointerException.

To prevent this, (and this is useful in *many* other areas of java programming), always check for null first before using that variable.
[This message has been edited by Mike Curwen (edited October 19, 2001).]
 
varkala prabhakar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear curwen,
thanx for you reply curwin and what you have said is perfectly right but in my cookies initialization program , setting cookie works fine but at the time of retrival i get the null exception. i dont know where the cookie is gone. and i have even set the cookie time as xxx.setMaxAge(10000).
thanx
prabhakar.
 
varkala prabhakar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My code is some thing like this

Cookie []cook = request.getCookies();
System.out.println(" Category ="+cat + " randum number:"+ randnum );
if(cook.length == 0)
{
System.out.println("is null ");
}
for(int i = 0; i < cook.length;i++){
if( cook[i].getName().equals(merid) ){
rand = (String)cook[i].getValue() ;
}
}
---------------------------------------
request.getCookies() works fine, i get the null exception in cook.length it get the value as null.

thanx
prabhakar.
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Mike Curwin already said, your request.getCookies() is returning null. It's either cookies are not allowed by the client or you are not specifying your cooke name correctly. You are getting the exception whe you try to call the length variable on a null array reference.

Bosun
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.getCookies() does *not* work fine. You are getting a null. When you try to access a field (.length) of a non-existent object, you get a NullPointerException. This is why you must *always* check an object for null before you attempt to access a field or method of that object. Do that, or pre-initialize the object to some state, other than null.

So as Bosun has said: either your setCookie() doesn't work like you think it does, or your cookie name is wrong. The third thought I had was your servlet that gets cookies is operating under a different domain than the one that sets your cookies.
 
raj sekhar
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey
Just modify you if condition to check null values of 'cook' too besides checking its length to zero...and you will know whats going on...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic