Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Name  RSS feed

 
Shravan Durvasula
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Ture Hefner
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gabriel Forro
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course )

I am ashamed
(I have to use .net at work, where base == super...I am sorry again)
 
Shravan Durvasula
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gabriel and Ture ... that helped.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!