aspose file tools*
The moose likes Servlets and the fly likes getServletContext() returning null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getServletContext() returning null" Watch "getServletContext() returning null" New topic
Author

getServletContext() returning null

Sameer Damle
Greenhorn

Joined: Jan 05, 2006
Posts: 14
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.


I think like I'm diagonally ||||<br />parked in a parallel universe.\\\\
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

How are you calling the Servlet? ie have you provided a servlet mapping?
Sameer Damle
Greenhorn

Joined: Jan 05, 2006
Posts: 14
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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 ]

[My Blog]
All roads lead to JavaRanch
Sameer Damle
Greenhorn

Joined: Jan 05, 2006
Posts: 14
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

Joined: Mar 06, 2001
Posts: 13459

D'oh, nice catch.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Jacques Traore
Greenhorn

Joined: Feb 23, 2010
Posts: 1
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.
 
Consider Paul's rocket mass heater.
 
subject: getServletContext() returning null