tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tom O'Connor" <>
Subject uninitialized instance variables in servlet
Date Mon, 30 Oct 2000 20:33:26 GMT
In the init() method of my Servlet class, I populate 2 String instance
variables that are later used in the doPost() method.  The String values are
populated by calling .getInitParameter() on the ServletConfig instance
passed into init().  I verify that the String returned from
.getInitParameter() != null inside init().

After tomcat starts, I start a client program that generates HTTP requests
aimed at the servlet.  More often than not, these requests fail with 500
Server Error as the response.  For each failed request, in
$TOMCAT_HOME/logs/tomcat.log there is a
NullPointerException and a stack trace, and the stack trace points me to a
line in my servlet where I invoke a method on the String instance variables
that should have been initialized in init().

This is *NOT* a configuration file issue.  I can say this because in the
same webapp, I have 2 servlets that each have the same servlet-class element
in web.xml.  The first servlet will generate failures (sometimes) but the
second servlet (which is the same class, but a different instance) will
succeed.  The only thing different between the two instances are the
init-param values in web.xml.  I also swap which servlet I invoke first and
it's the first servlet invoked that fails.

I've tried this using both Apache+mod_jk over Ajp13, and invoking the
servlet directly through Tomcat's HTTP adapter.

Doesn't the servlet spec say that no thread shall enter Servlet.service()
until Servlet.init() has successfully completed?  If that's true, how can my
instance variables be null?

The servlet looks like this:

public class S extends HttpServlet {
   private String n = null;
   private String v = null;
   public void init(ServletConfig config) throws ... {
      n = config.getInitParameter("n");
      v = config.getInitParameter("v");
      if (n == null) throw new UnavailableException();
      if (v == null) throw new UnavailableException();
	// snip
   public void doPost(...) throws ... {
      // this.n and this.v are used in here

View raw message