This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

ServletContext Null pointer Exception

 
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>

 
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
 
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
 
Author
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
 
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:

 
Marshal
Posts: 67424
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
 
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?
 
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
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic