Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext Null pointer Exception

 
Kc Sreejith
Greenhorn
Posts: 10
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API recommends overriding javax.servlet.GenericServlet.init() and using getServletContext()
 
Kc Sreejith
Greenhorn
Posts: 10
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am still getting the null pointer exception
please help me its urgent
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it's being deployed?
 
Kc Sreejith
Greenhorn
Posts: 10
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is covered in the Servlets FAQ.
 
Kc Sreejith
Greenhorn
Posts: 10
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot now its working
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sreejith jithu wrote:Thanks a lot now its working

What did you change to get it working?
 
dignesh trivedi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sreejith jithu wrote:Thanks a lot now its working


You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic