• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Checking if session exists problem

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic