Why is it that the below servlet throws a null pointer exception when the init(ServletConfig config) is overridden instead of init() ?
Joined: Apr 16, 2004
the HttpServlet has a private member (type of ServletConfig). This private member is initialized in the init(ServletConfig) method. The getServletName() calls the ServletConfig's getServletName() function, so it uses the private member (which is null, if it is not initialized).
If You override this function, make sure do it as it follows:
Joined: Nov 14, 2004
Right. If you override the init(ServletConfig sc) method in a Servlet, you must still call the overridden method in order to initialize the instance varible that keeps a handle to the ServletConfig. However, you use the "super" keyword to do this, not "base":