aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes getServletContext()  gives NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "getServletContext()  gives NullPointerException" Watch "getServletContext()  gives NullPointerException" New topic
Author

getServletContext() gives NullPointerException

Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

When I try to invoke getServletContext() method it gives a null pointer exception ? What can be the reason ?


my web.xml extract
<servlet>
<servlet-name>TestingServlet</servlet-name>
<servlet-class>com.tt.serlvlets.TestingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestingServlet</servlet-name>
<url-pattern>/servlets/test.exe</url-pattern>
</servlet-mapping>

code extract

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


synchronized (getServletConfig().getServletContext()) {
System.out.println("INNNNNNNNNNNNNNN");
}


calling extract

<a href="/ApacheProject/servlets/test.exe?param2=read" >Application Variable Set</a>





11.Ara.2008 18:18:09 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet TestingServlet threw exception
java.lang.NullPointerException
at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:160)
at com.tt.serlvlets.TestingServlet.doPost(TestingServlet.java:86)
at com.tt.serlvlets.TestingServlet.doGet(TestingServlet.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
cesar valencia
Ranch Hand

Joined: Oct 14, 2008
Posts: 33
doesn't seem like a problem with the servlet context, unless you've overriden service to service with doPost() only.

a link (as in href) will be serviced by doGet (normally). Try that first and let us know if it still throws Exception
[ December 11, 2008: Message edited by: cesar valencia ]

scjp5, scwcd5
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//doPost(request, response);
synchronized (getServletConfig().getServletContext()) {
System.out.println("INNNNNNNNNNNNNNN");
}

}


I have called doPost in my previous code to call doPost!! However I comment it and tryed to call getServletContext() in doGet still I am getting NullPointerException.

11.Ara.2008 20:09:30 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet TestingServlet threw exception
java.lang.NullPointerException
at com.tt.serlvlets.TestingServlet.doGet(TestingServlet.java:42)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter


Furthermore I can call getServletContext() method without getting NullPointerException in other servlet , it works fine ??? + I didn't override service method!

[ December 11, 2008: Message edited by: Anut Walidera ]
[ December 11, 2008: Message edited by: Anut Walidera ]
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Did you override the init(ServletConfig) method without calling super.init(ServletConfig)? The NPE is originating in GenericServlet.getServletContext(), which in turn calls getServletConfig()---the latter is setup upon initialisation by GenericServlet, and if you overrode that method but didn't set the variable yourself, it will still be null. If that doesn't do it, how about posting your entire Servlet class?


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

Charles Lyons

Thanks... Yes you are right...

I forgot that I chose to override option in my IDE than I realised with your saying that I didn't call super.init(ServletConfig) in my servlet class.



Thank you for your valueable response...


Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
For future reference, it's best to override only the init() method when extending GenericServlet (or HttpServlet), and not the init(ServletConfig). Then you can do your own initialisation without having to call superclass methods
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

Thanks...

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getServletContext() gives NullPointerException