Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NullPointerExceptions.....getServletContext(GenericServlet.java:209)

Status
Not open for further replies.

FreshmenProgrammer

Programmer
Jan 16, 2006
42
CA
Hello,

First I would like to say thanks in advance for the help!!

This is the error im getting.
SEVERE: null
java.lang.NullPointerException
at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:209)

I'm not sure if im not setting the application scope variable right or im not getting it right because i'm always getting a null value. Please let me know what you think. Thank you!

Code:
EISDAO eis = new EISDAO();
eis.setDBHost(config.getInitParameter("dbhost"));
//etc etc.. 

// i set the application scope variable in an init like this
config.getServletContext().setAttribute("eisdao", eis);

//
public SISDAO getEISDAO() {

return (EISDAO)getServletContext().getAttribute("eisdao");
	}
 
If you need to set session attributes ...

In a JSP, use

Code:
session.getAttribute("name");
and
Code:
session.setAttribute("name", "value");

In a servlet, you should use :

Code:
HttpSession session = request.getSession();
...
session.getAttribute("name");
...
session.setAttribute("name", "value");

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I need to set an Application Scope variable in a Abstract controller. So the whole app will have access to the EIS object.
 
Oops, sorry - missed the application level bit.

Unless someone knows of a better way, I would do it like this :

Create a servlet which handles initialization of application parameters.
Create a class which holds the variables you wish (or has a Hahsmap or table into which you can insert/retrieve arbitrary parameters). Make the member variable static, and create static accessor methods.

when the "Initialization" servlet fires up (ie override the servlets init() method), read your parameters, and use the class I mentioned before to set your parameters.

Then all your application code has to do is call the static methods on that class to retrieve various variables.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top