You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mario Imperato <me...@mclink.it> on 2000/04/15 12:23:44 UTC

Re: getServletContext() Error and init(..) and another question.

Craig, thank you for your note. It has been a kind of 'back to school' error. I think
I missed a basic point in OO programming and subclassing. 
I'm glad it was not just me. ;-)


Mario

On Fri, 14 Apr 2000 09:26:23 -0700, Craig R. McClanahan wrote:

>Mario Imperato wrote:
>
>> Hi folks, my apologies for the question. I'm experiencing a strange behaviour from a
>> test servlet I'm just developing. I wanted to have the servlet loaded at start up time
>> and added an init(ServletConfig config) method to initialize some stuff.
>> In the doGet method when I try to get the ServletContext I get a NullPointerException.
>> This doesn't happen if I remove the init method from the class.
>> What am I missing?
>>
>
>Without looking at the line of code where the NullPointerException is actually thrown,
>it's impossible to tell you exactly what the problem is.  However, I can describe a very
>common case that lots of people get caught on.
>
>At the beginning of your init(ServletConfig config) method, don't forget to call:
>
>    super(config);
>
>so that the base class initializer gets called as well.  Otherwise, later references to
>things like getServletContext() will fail.  This error happened to so many people that a
>new version of the init() method signature was added (with no arguments) so that you 
can
>override that one instead.  You'd do this:
>
>    public void init() throws ServletException {
>
>        ServletConfig config = getServletConfig();
>        ... all the rest of your current initialization ...
>
>    }
>
>> Mario
>>
>
>Craig McClanahan
>
>
>
>
>
>--------------------------------------------------------------------------
>To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
>For additional commmands, email: tomcat-user-help@jakarta.apache.org
>
>

Mario A. Imperato
e-mail: imperato@mclink.it
cell: 0335-5398828



Re: getServletContext() Error and init(..) and another question.

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Mario Imperato wrote:

> Craig, thank you for your note. It has been a kind of 'back to school' error. I think
> I missed a basic point in OO programming and subclassing.
> I'm glad it was not just me. ;-)
>

It sure isn't ... lots of the suggestions like this that I make are "voice of experience"
talking :-)

Craig