File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "getServletContext() returning null ???" Watch "getServletContext() returning null ???" New topic

getServletContext() returning null ???

sean cee
Ranch Hand

Joined: Oct 24, 2000
Posts: 115
Hi all,
My servlet has the following code in its doGet() method.
String adr = "/xxx/xxx.jsp";
ServletConfig sConf = getServletConfig(); // line1
ServletContext sCont = sConf.getServletContext(); //line2
RequestDispatcher dispatcher= sCont.getRequestDispatcher(adr);//line3
line 1 seems to work fine. (I originally had print statement between each line.. )
but line2 returns null..
'cos I get NullPointerException at line 3.
I am don't know what is wrong...

Sean <br />SCJP2, SCJP2p1.4, SCWCD
Satish Kasala

Joined: Dec 19, 2000
Posts: 16
Hi sean,
Can you be more specific about the servlet engine you are using. Some servlet engine doesn't seem to implement the JSDK specs completed. getServletContext() is one of the methods that is primarily based on the servlet engine.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Hi sean,
If we had overridden init(ServletConfig config)throws .. {...} method, then we shouldn't forget to call super.init(config); If we miss this line, then latter our servlet can't get hold of ServletConfig object and we will get NullPointerException. Is this your case?
maha anna
[This message has been edited by maha anna (edited December 21, 2000).]
sean cee
Ranch Hand

Joined: Oct 24, 2000
Posts: 115
Thanks both of you.
Especially Maha, you pinpointed the cause of my problem..
Thanks guys!!!
I agree. Here's the link:
subject: getServletContext() returning null ???
It's not a secret anymore!