aspose file tools*
The moose likes Java in General 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 » Java in General
Bookmark "ServletContext Null pointer Exception" Watch "ServletContext Null pointer Exception" New topic
Author

ServletContext Null pointer Exception

Kc Sreejith
Greenhorn

Joined: Jun 08, 2009
Posts: 10

Dear All,
When iam trying to get the context parameters iam geting an null pointer exception
code is like this

ServletContext context = getServletContext();
name = context.getInitParameter("dbname");

and in my web.xml
<context-param>
<param-name>dbname</param-name>
<param-value>name</param-value>
</context-param>

bhavesh bhatnagar
Greenhorn

Joined: Jun 15, 2009
Posts: 26
The above said Code is correct

Make sure that <context-param> is not within <servlet>...</servlet> block
and the <param-name> is same as the argument in getInitParameter();


Check for other code.. whether its correct.


Kc Sreejith
Greenhorn

Joined: Jun 08, 2009
Posts: 10

Thanks for the replay
i checked the code their is no mismatch
here my database connection parameters is in the web.xml and iam trying to access to a servlet using servletContext
when i created an object of this srevlet from a java class i got the null pointer exception
my sevlet is as follows
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

The API recommends overriding javax.servlet.GenericServlet.init() and using getServletContext()
Kc Sreejith
Greenhorn

Joined: Jun 08, 2009
Posts: 10

i am still getting the null pointer exception
please help me its urgent
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Are you sure it's being deployed?
Kc Sreejith
Greenhorn

Joined: Jun 08, 2009
Posts: 10

iam geting the values in the jsp page by using
application.getInitParameter()
but problem occurs when calling it from servlet
iam using eclipse and tomcat
bhavesh bhatnagar
Greenhorn

Joined: Jun 15, 2009
Posts: 26
You should try the code in the service method(get/post).. not in the init() method
{Dont overload init() method}
may be this way work
Sekyi Zulu
Greenhorn

Joined: May 24, 2009
Posts: 3
sreejith jithu wrote:Thanks for the replay
i checked the code their is no mismatch
here my database connection parameters is in the web.xml and iam trying to access to a servlet using servletContext
when i created an object of this srevlet from a java class i got the null pointer exception
my sevlet is as follows


According to the Servlet API:
init()... A convenience method which can be overridden so that there's no need to call super.init(config).

Without the call to super.init(config) in the init() method, any call to the GenericServlet's implementation of getInitParameter() or any other ServletConfig methods will throw a NullPointerException.

Modify your code to this:


__
or use the empty init() method version:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

This is covered in the Servlets FAQ.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kc Sreejith
Greenhorn

Joined: Jun 08, 2009
Posts: 10

Thanks a lot now its working
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
sreejith jithu wrote:Thanks a lot now its working

What did you change to get it working?


Thanks and Regards
dignesh trivedi
Greenhorn

Joined: Jul 06, 2009
Posts: 1
ServletContext context = getServletContext();
name = context.getInitParameter("dbname");

Maybe you must type cast to something like Integer or String or the best option Object
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
dignesh trivedi wrote:ServletContext context = getServletContext();
name = context.getInitParameter("dbname");

Maybe you must type cast to something like Integer or String or the best option Object

That should not be necessary as getInitParameter returns a String.
Sekyi Zulu
Greenhorn

Joined: May 24, 2009
Posts: 3
sreejith jithu wrote:Thanks a lot now its working


You're welcome
 
 
subject: ServletContext Null pointer Exception