aspose file tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Servlet Name Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "Servlet Name" Watch "Servlet Name" New topic
Author

Servlet Name

Shravan Durvasula
Greenhorn

Joined: Mar 06, 2004
Posts: 19
Hi all:

Why is it that the below servlet throws a null pointer exception when the init(ServletConfig config) is overridden instead of init() ?



Thanks,
-SkD
Gabriel Forro
Ranch Hand

Joined: Apr 16, 2004
Posts: 59
Hi,

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:


Gabriel
Ture Hefner
Greenhorn

Joined: Nov 14, 2004
Posts: 18
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":

// base.init(sc); // nope
super.init(sc); // yessiree

See http://java.sun.com/docs/books/tutorial/java/javaOO/methodbody.html


Ture
Gabriel Forro
Ranch Hand

Joined: Apr 16, 2004
Posts: 59
Of course )

I am ashamed
(I have to use .net at work, where base == super...I am sorry again)
Shravan Durvasula
Greenhorn

Joined: Mar 06, 2004
Posts: 19
Thanks Gabriel and Ture ... that helped.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Servlet Name
 
Similar Threads
Whats the o/p of this servlet pgm?
Which one gets first executed ?????????
why super.init() is used??
Servletcontext object to actionservlet in struts
when does a servlet clas get servletness ?