dev@jersey.java.net

Re: [Jersey] null pointer exception on resource class access HttpServletRequest?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 21 Apr 2010 16:46:58 +0200

Hi James,

First time i have seen that type of error. Not sure why it occurs. It
is rather fundamental and implies that Jersey's context is set up with
a null request and I do not see how that can happen (see the code at
the end of the email).

Can you send me your project (email me directly if disclosure is an
issue) as i think it will be a lot easier to work out what might be
the problem.

Also sending a server log would be most helpful.

Paul.

     public void handleRequest(ContainerRequest request,
ContainerResponseWriter responseWriter)
             throws IOException {
         final ContainerResponse response = new ContainerResponse(
                 this,
                 request,
                 responseWriter);
         handleRequest(request, response);
     }

     public void handleRequest(ContainerRequest request,
ContainerResponse response) throws IOException {
         final WebApplicationContext localContext = new
                 WebApplicationContext(this, request, response);

         context.set(localContext);
         try {
             _handleRequest(localContext, request, response);
         } finally {
             PerRequestFactory.destroy(localContext);
             closeableFactory.close(localContext);
             context.set(null);
         }
     }


On Apr 21, 2010, at 4:29 PM, James Russo wrote:

> Hello Everyone,
>
> I am getting a null pointer exception while trying to use jersey
> and spring. I think it has something to do with the resource class
> being "singleton" scoped. I am using: 1.1.5.1
>
> java.lang.NullPointerException
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationContext.getProperties(WebApplicationContext.java:114)
> at
> com
> .sun
> .jersey
> .server
> .impl.resource.PerRequestFactory.destroy(PerRequestFactory.java:75)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:943)
>
> I have the RequestContextListener in my web.xml..
>
> I have my resource class with @Scope("singleton") and then I have
> some resource methods which the following as a parameter:
>
> @Context HttpServletRequest req
>
> So, I would think that the class can be singleton since the request
> is coming via a method parameter. I need the request object to get
> at some header values. I have tried changing to @Scope("request"),
> but it has the same result.
>
> Anyone point me in the right direction here before I waste all day
> on this?
>
> thanks,
>
> -jr
>