RSS Feed for This PostCurrent Article

Apache Tomcat Servlet Error in GenericServlet.getServletName

Here is the error I got for one of the Java servlet

   1: 2008-09-30 02:36:50,915 | ERROR | [StandardWrapperValve:260 invoke | http-8881-Processor20] - Servlet.service() for servlet myServlet threw
   2:  exception
   3: java.lang.NullPointerException
   4:         at javax.servlet.GenericServlet.getServletName(

The servlet is configured to be invoked upon certain URL pattern in web.xml. In other word, it will be called by the servlet container

If you look at the JavaDoc of GenericeServlet, it has 2 init methods, one without any parameters, another accept ServletConfig as the parameter

   1: public  void init() throws ServletException;
   3: public void init(ServletConfig config)throws ServletException;

From the JavaDoc, for init() method

A convenience method which can be overridden so that there’s no need to call super.init(config).

Instead of overriding #init(ServletConfig) , simply override this method and it will be called by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via #getServletConfig.

For init(ServletConfig) method

Called by the servlet container to indicate to a servlet that the servlet is being placed into service. See Servlet#init .

This implementation stores the ServletConfig object it receives from the servlet container for later use. When overriding this form of the method, call super.init(config).

Obviously when writing the servlet, and overriding the init(config) method, supert.init is not called. That is why the NullPointerException is always there.

Trackback URL

Sorry, comments for this entry are closed at this time.