File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getServletContext() gives NullPointerException

 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
cesar valencia
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 220
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks...

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic