servlet.getServletContext() always null...why?

2001-08-06 Thread Jon Crater

i have a base action class in which i declare a variable of type 
ServletContext:

public abstract class BaseAction extends Action
{
   protected ServletContext ctx = servlet.getServletContext();
   ...
}

yet i always get a NullPointerException on this line when struts tries to 
instantiate a subclass of this base class.  why?

2001-08-06 01:13:55 - path=/bc :action: Error creating Action instance for 
path '/Login', class name 'com.gs.bc.action.LoginAction' - 
java.lang.NullPointerException
at com.gs.bc.action.BaseAction.init(BaseAction.java:15)
at com.gs.bc.action.LoginAction.init(LoginAction.java:16)
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Class.java:237)
at 
org.apache.struts.action.ActionServlet.processActionCreate(ActionServlet.java:1631)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1576)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:509)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
at org.apache.tomcat.core.Handler.service(Handler.java:287)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at 
org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at 
org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
at 
org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at 
org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
at java.lang.Thread.run(Thread.java:484)


_
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp




Re: servlet.getServletContext() always null...why?

2001-08-06 Thread Craig R. McClanahan



On Mon, 6 Aug 2001, Jon Crater wrote:

 i have a base action class in which i declare a variable of type 
 ServletContext:
 
 public abstract class BaseAction extends Action
 {
protected ServletContext ctx = servlet.getServletContext();
...
 }
 
 yet i always get a NullPointerException on this line when struts tries to 
 instantiate a subclass of this base class.  why?
 

Because instance variable initialization expressions like this are
executed (effectively) when the constructor is called -- so Struts has not
yet had time to initialize the servlet property.

I suggest you override setServlet() instead:

protected ServletContext ctx = null;

public void setServlet(ActionServlet servlet) {
  super(servlet);
  if (servlet != null)
ctx = servlet.getServletContext();
  else
ctx = null;
}


Craig