aspose file tools*
The moose likes Servlets and the fly likes ServletContext null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContext null pointer exception" Watch "ServletContext null pointer exception" New topic
Author

ServletContext null pointer exception

dinesh laxman kumar
Greenhorn

Joined: Jul 11, 2012
Posts: 19
Hi people,

Code:


NullPointerException at highlighted line.

Any views and suggestion regarding code optimization.

Thanks in advance.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

why context is null?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and Welcome to JavaRanch rambo khan

I have applied code tag to your code. how good now! please use code tag while posting code in future
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
You do lose the highlighting with code tags, however: the line in question is now no 38.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
You ought to have suspected something because you would have a printout of “null before” just before the Exception occurs.
dinesh laxman kumar
Greenhorn

Joined: Jul 11, 2012
Posts: 19
Will make note of all your suggestions for the future..

Exactly, i get NPE at context.setAttribute("ds",ds);
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

What makes you think that context is ever initialized?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You have a context variable, but it never gets assigned a value (at least not in the code you posted). If you do assign a value to the context variable you do it someplace else and it happens after the getDs() method gets called.

Also, you should be aware that storing static content in servlet applications can be a dicey situation. There are multiple class loaders, and any one instance may be loaded from different class loaders and therefor have a different static context (it would appear as a different class, more or less). Also, classes can be loaded and unloaded over the course of an application run. You should probably not store the ServletContext as a static variable, but either pass it in to the constructor or pass it in to the method(s) which require it.

Finally, on a design note, since the class you are writing appears to be a Data Access Object, I am not sure making it dependent on the ServletContext, and by extension locking it into the Servlet environment, is a good idea. It would be better to generalize here: don't require the ServletContext, instead pass into this class whatever the ServletContext is supposed to give you. If the only purpose is to provide a scope to store a DataSource reference in then you should probably just let whoever calls the getDs() handle storing the value. Also in the design realm - I wouldn't want my DAO getXXX() method have a side-effect so large as to set a global state like setting a value in the ServletContext would do. I would want that to happen explicitly and in the controller code (Serlvet-aware portion of your code).


Steve
dinesh laxman kumar
Greenhorn

Joined: Jul 11, 2012
Posts: 19
Very thanks Steve on your thoughts

I tried passing context through method parameters, it worked

If you dont mind, I am quite new at this.
And if you could please clarify on the last part

Finally, on a design note, since the class you are writing appears to be a Data Access Object, I am not sure making it dependent on the ServletContext, and by extension locking it into the Servlet environment, is a good idea. It would be better to generalize here: don't require the ServletContext, instead pass into this class whatever the ServletContext is supposed to give you. If the only purpose is to provide a scope to store a DataSource reference in then you should probably just let whoever calls the getDs() handle storing the value. Also in the design realm - I wouldn't want my DAO getXXX() method have a side-effect so large as to set a global state like setting a value in the ServletContext would do. I would want that to happen explicitly and in the controller code (Serlvet-aware portion of your code).


Do I have to create a separate class to access and save the datasource object?
And the Servlet aware implementation?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

That depends on what you need from the ServletContext. If all you want to do is store the DataSource in the ServletContext, then I would not write any class for that, what I would do instead is let whoever calls getDs() store the DataSource:


If the ServletContext provides some information to the DAO class then you would instead pass that information into the DAO:









But if you needed to store lots of things in the ServletContext and didn't want to be tied to the Servlet environment, you would create a storage interface and wrapper. Something like:


Then it would be trivial to replace the ServletContextScope with a SessionScope if you find you need to store the datasource in a user-specific scope. Or replace it with a LocalMapScope if you wanted to use the same DAO in a local application or be able to test it away from the servlet container.

The idea is if the working class (the Dao) doesn't absolutely need to know that it is running in a servlet environment then you should do what you can to make it independent on servlet environment, and that means no direct references to anything Servlet related.
dinesh laxman kumar
Greenhorn

Joined: Jul 11, 2012
Posts: 19
Thanks a million

Again great inputs, more than expected.

Cheers
 
jQuery in Action, 2nd edition
 
subject: ServletContext null pointer exception