You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by Craig Mason-Jones <cr...@lateral.co.za> on 2006/11/29 20:36:44 UTC

NullPointerException in org.apache.catalina.connector.Request.setAttribute(Request.java:1376)

Hi,

I posted a request for assistance earlier with this error, where the 
Context in the Catalina connector was becoming null. I found a couple of 
other messages posted where others had encountered a similar problem, so 
I thought I would post a quick explanation of what I was doing that was 
causing the error:

I created a class that stored the Request object, and stored that class 
in the session.

class Desktop {
    protected HttpServletRequest request;
    protected Desktop(HttpServletRequest request) {
       this.request = request;
    }

    public static Desktop getDesktop(HttpServletRequest request) {
       Desktop desktop = (Desktop) 
request.getSession().getAttribute("desktop");
       if (desktop==null) return new Desktop(request);
       // Fix goes here (see later)
       return desktop;
    }

    public void setAttribute(String name, String value) {
       request.setAttribute(name, value);
    }
}
 
Now, of course this will work as long as the cached request object is 
valid - but that is really up to the web server. I'm not working with 
the current request object, but with the one that was cached in the 
session when the Desktop was created. And at some point that won't work, 
though the error appears sporadically because the Context object in the 
Request only goes out of scope at the discretion of the server (I have 
no idea how that is determined).

The fix goes in the marked line above:
    desktop.request = request;

And it works fine. Stupid error on my part, but a monster to locate. I 
hope someone finds this explanation useful ;-)

All the best,
Craig