----- Original Message -----
From: "Jim Cheesman" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, April 18, 2001 12:04 PM
Subject: [cactus] ServletContext causes NullPointerException


> Everytime I call the doGet() method of my servlet I get a
> NullPointerException - it seems that the servlet has no context (i.e. no
> ServletConfig), and the call to getServletContext() fails.
>
> As my code relies on the following line, this is a problem:
> getServletContext().getRequestDispatcher(jspURL).forward(request,
response);
>
>
> Is there a workaround or, as is quite possible, am I just being immensely
> stupid?
>

In order to test methods that uses getServletContext() or getServletConfig()
or any method inherited from GenericServlet, you have to initialize the
servlet first by executing :

public void testDoGet() throws Exception
{
         CentralLoggingServlet servlet = new CentralLoggingServlet();
         servlet.init(config); // Needed to initialize the servlet config
object
         servlet.doGet(request, response);
}

This is described in the Jyve FAQ (which is unfortunately not working
currently) and there is a sample on how to do that in the Cactus Sample (in
package org.apache.commons.cactus.sample, class SampleServletConfig and
TestSampleServletConfig) if you wish to have a look.

Where do you think I should describe this on the web site so that users will
see it right away ? In a FAQ page on the web site as it was before ? In the
"Servlet Sample" web page ?

Thanks
Vincent.


Reply via email to