jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jose Alfonso Martinez <tril...@linuxcenter.com.mx>
Subject calling getServletContext() in a servlet - how to??
Date Mon, 11 Aug 2003 17:43:00 GMT
Hello,

I am testing a servlet, just the init() method like this:

	public void testPostMethod() throws ServletException
        {
                InscribeServlet inscribeServlet = new InscribeServlet();
                inscribeServlet.init(config);
        }

I get a java.lang.NullPointerException when calling getServletContext()...

in the init() method, my servlet calls getServletContext().getRealPath(). I guess there is
where the exception occurs. It could also be in a call to get the DataSource via InitialContext.
Attached is the stack trace and the init() method of my Servlet.

Does anyone knows what am I doing wrong???? thanks in advance.

Jose


Stack Trace
============

test2:
    [junit] Running com.cevex.servlet.TestInscribeServlet
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 1.562 sec
    [junit] Testsuite: com.cevex.servlet.TestInscribeServlet
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 1.562 sec

    [junit] Testcase: testPostMethod took 1.514 sec
    [junit] 	Caused an ERROR
    [junit] null
    [junit] java.lang.NullPointerException
    [junit] 	at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:204)
    [junit] 	at com.cevex.servlet.InscribeServlet.init(Unknown Source)
    [junit] 	at javax.servlet.GenericServlet.init(GenericServlet.java:256)
    [junit] 	at com.cevex.servlet.TestInscribeServlet.testPostMethod(Unknown Source)
    [junit] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [junit] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    [junit] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    [junit] 	at org.apache.cactus.internal.server.ServerTestCaseDelegate.runServerTest(ServerTestCaseDelegate.java;org/apache/cactus/util/log/LogAspect.aj[1k]:214)
    [junit] 	at org.apache.cactus.ServletTestCase.runTest(ServletTestCase.java:280)
    [junit] 	at org.apache.cactus.ServletTestCase.runBare(ServletTestCase.java:251)
    [junit] 	at org.apache.cactus.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:156)
    [junit] 	at org.apache.cactus.server.AbstractWebTestController.dispatch133_handleRequest(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:130)
    [junit] 	at org.apache.cactus.server.AbstractWebTestController.around133_handleRequest(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
    [junit] 	at org.apache.cactus.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:101)
    [junit] 	at org.apache.cactus.server.ServletTestRedirector.dispatch160_doPost(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:125)
    [junit] 	at org.apache.cactus.server.ServletTestRedirector.around160_doPost(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
    [junit] 	at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:109)
    [junit] 	at org.apache.cactus.server.ServletTestRedirector.dispatch159_doGet(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:96)
    [junit] 	at org.apache.cactus.server.ServletTestRedirector.around159_doGet(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
    [junit] 	at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:92)
    [junit] 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
    [junit] 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    [junit] 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
    [junit] 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
    [junit] 	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    [junit] 	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    [junit] 	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    [junit] 	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    [junit] 	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    [junit] 	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    [junit] 	at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
    [junit] 	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    [junit] 	at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
    [junit] 	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
    [junit] 	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:509)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
    [junit] 	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    [junit] 	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    [junit] 	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    [junit] 	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    [junit] 	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    [junit] 	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
    [junit] 	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:432)
    [junit] 	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:386)
    [junit] 	at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:534)
    [junit] 	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:530)
    [junit] 	at java.lang.Thread.run(Thread.java:536)




And the init() method of my Servlet
===================================
  public void init() throws CevexServletException
  {
    try
    {
      // get the initial context
      Context initCtx = new InitialContext();
      Context envCtx = (Context) initCtx.lookup("java:comp/env");

      // get the datasource
      ds = (DataSource) envCtx.lookup("jdbc/cevexDB");

      /* something is worng with the mail session, check later */
      //session = (Session) envCtx.lookup("mail/Session");         

      // initialize new mail seession
      Properties props = new Properties();
      props.put("mail.smtp.host", "localhost");
      Session session = Session.getDefaultInstance(props, null);
      session.setDebug(false);

      // Read email strings from files
      String realPath = getServletContext().getRealPath("/");
      mailInscripcionMasc = new CevexMail(session, realPath + getInitParameter("mailInscripcionMasc"));
      mailInscripcionFem  = new CevexMail(session, realPath + getInitParameter("mailInscripcionFem"));
      mailInscripcionConInvitacionMasc = new CevexMail(session, realPath +
                                             getInitParameter("mailInscripcionConInvitacionMasc"));
      mailInscripcionConInvitacionFem = new CevexMail(session, realPath +
                                             getInitParameter("mailInscripcionConInvitacionFem"));

    } // end try 
    .... catch statements
 }	

Mime
View raw message