Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8931569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:08:34+00:00 2026-06-15T09:08:34+00:00

I am trying to override init(ServletConfig config) method.My code is: public void init(ServletConfig config)

  • 0

I am trying to override init(ServletConfig config) method.My code is:

 public void init(ServletConfig config) throws ServletException {
    ServletContext sc = getServletContext(); // ----- NullPointerException
}

this is giving NullPointerException .

If i modify it as:

   public void init(ServletConfig config) throws ServletException {
    ServletContext sc = config.getServletContext(); // ----- works fine
}

This works fine.
I know that we should override init() method and not init(ServletConfig config) but
Can anybody give me proper reason as why this is happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:08:35+00:00Added an answer on June 15, 2026 at 9:08 am

    Compare the documentation for init(ServletConfig):

    public void init(ServletConfig config)throws ServletException
    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).
    

    And compare that with the documentation for init():

    public void init() throws ServletException
    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().
    

    When overriding init(ServletConfig), the first thing that must be done is to call:

    super.init(config);
    

    If you do this then calling directly to getServletContext() in your method will no longer result in an NPE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to render a component from withing another component. public override void Render()
I am trying to override the init method for a UIAlertView and customize it
I'm trying to decrypt an encrypted image in Android using this code: public class
im trying to override the add() but it will not compile public class AVLTree<E
I'm trying to override a method inside a jquery widget. The method can be
Why does this init method return an object out of scope? Using XCode 4.2,
im trying to setup two listeners for one button. Here is my code: This
I'm trying to override the getDescriptiveName() method in com.liferay.portal.model.Group I found a wrapper (com.liferay.portal.model.GroupWrapper),
I'm trying to sub-class db.Property and override the set method to implement stuff like
What I was trying to do was to override the save method of one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.