This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Servlets and the fly likes Null exception retrieving session attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Servlets
Bookmark "Null exception retrieving session attribute" Watch "Null exception retrieving session attribute" New topic

Null exception retrieving session attribute

Mikey Kelly

Joined: Aug 09, 2005
Posts: 18
I'm doing this from within my servlet:

I get a null pointer exception at the line marked **. The "sessionBean" attribute is not initialised at first, but shouldn't the expression just put null into sB without giving an exception?
What is a better approach?
Mikey Kelly

Joined: Aug 09, 2005
Posts: 18
Sorry, I think I have the wrong line for the exception - time to dig around in Tomcat...

Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121
The only way that line can throw a NullPointerException is when session is null. How do you get the session? If you have created it using request.getSession(false), and if a session does not already exist, it won't create a session and the 'session' object will be null.

keep smilin :: sravan<br /><a href="" target="_blank" rel="nofollow">I scribble here</a>
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Null exception retrieving session attribute
It's not a secret anymore!