• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getServletContext() returning null

 
Sameer Damle
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to excecute the following code it gets compiled but when i call that servlet i am getting null pointer exception.

the code is as follows.....


thanks in advanced.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you calling the Servlet? ie have you provided a servlet mapping?
 
Sameer Damle
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i have provided mapping for the servlet


i am calling the sevlet using the url

http://172.20.44.85:7001/sameer/Versionservlet
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have overridden init(ServletConfig) so you should store the servletConfig somewhere, or call the parent's init method.

In your init():
Either ctx = cfg.getServletContext();
Or super.init(cfg);


Overriding the init() method will prevent the default mechanism, so HttpServlet won't store the ServletConfig for you. That's why getServletContext() will cause NullPointerException.
[ January 26, 2006: Message edited by: Satou kurinosuke ]
 
Sameer Damle
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks satou...

i just called super.init() from the overridden init()....

that solved the purppose...

thanks once again for prompt help....
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D'oh, nice catch.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sameer Damle

Just update the servlet FAQ. Take a look at it to have a better understanding of init() and init(ServletConfig config).

I request moderators to erase the question marks and make it look better.
 
Jacques Traore
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:You have overridden init(ServletConfig) so you should store the servletConfig somewhere, or call the parent's init method.

In your init():
Either ctx = cfg.getServletContext();
Or super.init(cfg);


Overriding the init() method will prevent the default mechanism, so HttpServlet won't store the ServletConfig for you. That's why getServletContext() will cause NullPointerException.
[ January 26, 2006: Message edited by: Satou kurinosuke ]


Thank you very much Mr Verré.
Four years later, you always helps someone else.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic