jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Vincent Massol" <vmas...@pivolis.com>
Subject RE: getServletContext() throws a NullPointerException
Date Tue, 11 Nov 2003 17:59:16 GMT
Hi David,

Are you sure you've called init(config)?

See http://jakarta.apache.org/cactus/writing/howto_testcase_servlet.html
(section "Tips and tricks").

Thanks
-Vincent

> -----Original Message-----
> From: David Lemieux (QC/EMC) [mailto:David.Lemieux@ericsson.com]
> Sent: 11 November 2003 17:20
> To: 'Cactus Users List' (E-mail)
> Subject: getServletContext() throws a NullPointerException
> 
> Hi,
> 
> If I call the servlet directly, everything work fine. But if I run it
with
> cactus, it throws a NullPointerException.
> I've included the junit ant log, the test class and the class under
test.
> There is nothing in the server log.
> 
> The appserver is weblogic 8.1
> Cactus version is 1.5rc1
> 
> 
> /************************************************/
> /************************************************/
>        [junit] Testcase: testExample took 1.016 sec
>        [junit] Testcase: testXXX took 0.094 sec
>        [junit] Testcase: testShouldNotBeSuccess took 0.031 sec
>        [junit] Testcase: testResultSessionBean took 0.219 sec
>        [junit] Testcase: testResponseHelloServlet took 0.187 sec
>        [junit] 	Caused an ERROR
>        [junit] null
>        [junit] java.lang.NullPointerException
>        [junit] 	at
>
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:205)
>        [junit] 	at
> XXX.applications.sampleapplication.HelloServlet.processRequest(Unknown
> Source)
>        [junit] 	at
>
XXX.applications.sampleapplication.TestHelloServlet.testResponseHelloSer
vl
> et(Unknown Source)
>        [junit] 	at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method)
>        [junit] 	at
>
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:
> 39)
>        [junit] 	at
>
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Im
> pl.java:25)
>        [junit] 	at
>
org.apache.cactus.ServletTestCase.runCactusTest(ServletTestCase.java:295
)
>        [junit] 	at
> org.apache.cactus.ServletTestCase.runBare(ServletTestCase.java:263)
>        [junit] 	at
>
org.apache.cactus.server.AbstractWebTestCaller.doTest(AbstractWebTestCal
le
> r.java:156)
>        [junit] 	at
>
org.apache.cactus.server.AbstractWebTestController.handleRequest_aroundB
od
> y0(AbstractWebTestController.java:130)
>        [junit] 	at
>
org.apache.cactus.server.AbstractWebTestController.handleRequest_aroundB
od
> y1$advice(AbstractWebTestController.java:158)
>        [junit] 	at
>
org.apache.cactus.server.AbstractWebTestController.handleRequest(Abstrac
tW
> ebTestController.java)
>        [junit] 	at
>
org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(Servle
tT
> estRedirector.java:138)
>        [junit] 	at
>
org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody3$advice
(S
> ervletTestRedirector.java:158)
>        [junit] 	at
>
org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirec
to
> r.java)
>        [junit] 	at
>
org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody0(Servlet
Te
> stRedirector.java:109)
>        [junit] 	at
>
org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody1$advice(
Se
> rvletTestRedirector.java:158)
>        [junit] 	at
>
org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirect
or
> .java)
>        [junit] 	at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
>        [junit] 	at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
>        [junit] 	at
>
weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(Se
rv
> letStubImpl.java:1053)
>        [junit] 	at
>
weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.
ja
> va:387)
>        [junit] 	at
> weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:28)
>        [junit] 	at
>
weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:
27
> )
>        [junit] 	at
>
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.r
un
> (WebAppServletContext.java:6316)
>        [junit] 	at
>
weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSu
bj
> ect.java:317)
>        [junit] 	at
>
weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118
)
>        [junit] 	at
>
weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServl
et
> Context.java:3622)
>        [junit] 	at
>
weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.
ja
> va:2569)
>        [junit] 	at
> weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
>        [junit] 	at
> weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)
>        [junit]
>        [junit] Testcase: testResponseHelloServlet
>        [junit] TEST XXX.AllTest FAILED
> 
> /******************************************************************/
> /******************************************************************/
> 
> package XXX.applications.sampleapplication;
> 
> import java.io.IOException;
> 
> import javax.servlet.ServletException;
> 
> import junit.framework.Test;
> import junit.framework.TestSuite;
> 
> import org.apache.cactus.ServletTestCase;
> import org.apache.cactus.WebRequest;
> import org.apache.cactus.WebResponse;
> 
> import XXX.test.genericframework.log.LoggerFactory;
> 
> public class TestHelloServlet extends ServletTestCase{
> 
> 	...
> 
> 	public void beginResponseHelloServlet(WebRequest myWebRequest){
> 
>
LoggerFactory.getInstance().logInfo("beginResponseHelloServlet","bef
> ore addParameter");
> 		myWebRequest.addParameter("username","David");
> 	}
> 
> 	public void endResponseHelloServlet(WebResponse theResponse){
> 
>
LoggerFactory.getInstance().logInfo("endResponseHelloServlet","in
> endXXX");
> 		assertTrue("Assert that the submitted name is the one on
the
> page",theResponse.getText().indexOf("David") != -1);
> 	}
> 
> 
> 	public void testResponseHelloServlet(){
> 
>
LoggerFactory.getInstance().logInfo("testResponseHelloServlet","test
> ");
> 		HelloServlet myHelloServlet = new HelloServlet();
> 
> 		assertNotNull("servlet not null",myHelloServlet);
> 		assertNotNull("request not null",request);
> 		assertNotNull("response not null",response);
> 
> 		try {
> 			myHelloServlet.init();
> 			myHelloServlet.processRequest(request,response);
> 		} catch (ServletException e) {
> 			// XXX Auto-generated catch block
> 			e.printStackTrace();
> 			fail(e.toString());
> 		} catch (IOException e) {
> 			// XXX Auto-generated catch block
> 			e.printStackTrace();
> 			fail(e.toString());
> 		}
> 	}
> }
>
/***********************************************************************
*/
>
/***********************************************************************
*/
> 
>     public void processRequest(HttpServletRequest
> request,HttpServletResponse response) throws ServletException,
IOException
> {
> 
> 	  System.out.println("Debut");
>       response.setContentType("text/html");
>       response.setBufferSize(8192);
>       PrintWriter out = response.getWriter();
> 
>       String message = null;
> 
> 		... // Set message using session bean
> 
> 		if(message == null){
> 			message = "";
> 		}
>       // then write the data of the response
>       out.println("<html>" +
>                   "<head><title>Hello</title></head>");
> 
> 		// then write the data of the response
>       out.println("<body  bgcolor=\"#ffffff\">" +
>          "<img src=\"/mml/images/mmllogo.gif\"  width=\"204\"
> height=\"100\">" +
> 		"<h2>" + message + "</h2>" +
>          "<form method=\"get\">" +
>          "<input type=\"text\" name=\"username\" size=\"25\">" +
>          "<p></p>" +
>          "<input type=\"submit\" value=\"Submit\">" +
>          "<input type=\"reset\" value=\"Reset\">" +
>          "</form>");
> 
>       String username = request.getParameter("username");
>       if ( username != null && username.length() > 0 ) {
> /*	THE NEXT LINE THROWS THE EXCEPTION	*/
> /*******/ RequestDispatcher dispatcher =
>
getServletContext().getRequestDispatcher("/sampleapplication/hellorespon
se
> ");
> 
>             if (dispatcher != null){
> 				dispatcher.include(request, response);
>             }
> 
>       }
>       out.println("</body></html>");
>       out.close();
> 	  System.out.println("Fin");
>     }
>
/***********************************************************************
**
> *********/
>
/***********************************************************************
**
> *********/
> 
> 
> David Lemieux
> 



Mime
View raw message