File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Checking if session exists problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Checking if session exists problem" Watch "Checking if session exists problem" New topic
Author

Checking if session exists problem

shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Hi Guys

I have the following code:



The above code checks if a session exists. If it doesnt then the page should redirect to the login page. The problem is whenever the session cannot be found I get a 500 java.lang.NullPointerException error. Does anyone know where I am going wrong ?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by shaf maff:
Hi Guys

I have the following code:



The above code checks if a session exists. If it doesnt then the page should redirect to the login page. The problem is whenever the session cannot be found I get a 500 java.lang.NullPointerException error. Does anyone know where I am going wrong ?


What that code does is check if the user has signed in yet by checking if a token was placed in a session. To check if a session exists you need to do this:


Or you could force a new session to be made if one doesn't already exist, and check it for the user token (which won't be there if there was no session to start with):



Steve
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Hi Steve,

I tried your suggestion and I still get the same error when I force tomcat to expire the session. See error followed by the code:





code:
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Your code should include a return after the sendRedirect.
Bill
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Do you mean a return false after each statment ? I tried that and it gives me a cannot return value from a method whose result type is void.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21


Then either lettersession or user is null when you try to use it. Figure out which and prevent it from causing an exception.


Originally posted by shaf maff:
Do you mean a return false after each statment ? I tried that and it gives me a cannot return value from a method whose result type is void.


No, not return false. Just return; The idea is that once you do a sendRedirect you want to make sure that none of the rest of the code in the method (the code after the if/else statements) gets executed.



An alternative is to put all your code inside the if/else clauses so there doesn't need to be artificial returns:


Which you do depends on style.
[ September 26, 2008: Message edited by: Steve Luke ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checking if session exists problem