This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Servlets and the fly likes getServletContext null pointer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getServletContext null pointer" Watch "getServletContext null pointer" New topic

getServletContext null pointer

Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
Please can ya help me out yet again wise ones

I have two servlets, and 1 bean. In one servlet i set the values of the bean and store them in the servlet context. In another servlet i try to get the ServletContext back, But it allways returns a null pointer error...

Here is the code, thanks in advance for help.

And finally the bean

If you got this far, thanks for looking
[ December 24, 2005: Message edited by: Bear Bibeault ]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13027
1. are both servlets in the same "Web application"?
are both defined in one web.xml?
2. you left out one crucial bit of information - which reference is null?
is it bob? if so maybe your init method is at fault and you are missing the ServletConfig reference
is it dbParam? if so maybe the two servlets are not really sharing the same context
3. are you relying on the "invoker" servlet at any point? See this Ranch FAQ if you are.
Al Hollis
Ranch Hand

Joined: Dec 12, 2005
Posts: 60
well ive kinda sorted the problem now using instance rather that servlet context to store it...

However i would like to switch back to servlet context as my tutor seems to prefer this

Bob is the place where it throws a null reference back at me,
They are both in one web application, i havnt used any init meathods so not sure how that can be the case. Its really got me stumped, ohh and no invoker meathod either...

Thanks for looking!
[ December 24, 2005: Message edited by: Al Hollis ]
Paul Clapham

Joined: Oct 14, 2005
Posts: 19864

Bob is the place where it throws a null reference back at me
This is rather ambiguous. Are you saying that the variable "bob" contains null? Surely not. Why don't you tell us which line of code throws the exception? Your stack trace should tell you the exact line number.
wood burning stoves
subject: getServletContext null pointer
It's not a secret anymore!