Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking if session exists problem

 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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):

 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code should include a return after the sendRedirect.
Bill
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic