users@jersey.java.net

[Jersey] Inexplicable NullPointerException

From: Camel Christophe <ccamel_at_cls.fr>
Date: Thu, 31 Mar 2011 10:22:48 +0200

Hi,

I am facing an exception of type NullPointerException that I can not explain. I develop a big application that worked very well so far. Now I have made ​​many changes, including changing the version of jersey (from 1.3 to 1.6), and the application fails to launch.

Exception is the following:
Caused by: java.lang.NullPointerException
        at com.sun.jersey.server.wadl.generators.resourcedoc.WadlGeneratorResourceDocSupport.createParam(WadlGeneratorResourceDocSupport.java:324)
        at com.sun.jersey.server.wadl.WadlBuilder.generateParam(WadlBuilder.java:266)
        at com.sun.jersey.server.wadl.WadlBuilder.generateResource(WadlBuilder.java:300)
        at com.sun.jersey.server.wadl.WadlBuilder.generateResource(WadlBuilder.java:271)
...

It happens the following thing:

Let's say I have a resource declared like this (I'm using spring as ioc):

@Component
@Scope("singleton")
@Path("catalogcontexts")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public class CatalogContextsResource {
    @Resource
    private AccountService accountService;

    ....
}

When building resources, jersey relies on the IntrospectionModeller to do the job. It calls the createResource() method, who in turn calls workOutFieldsList() method. This method lists all the fields of the resource class, and in our example, find the following field: accountService.

From this field, the IntrospectionModeller creates a parameter with the following arguments:
concreteClass = x.y.z.CatalogContextsResource
declaringClass = x.y.z.CatalogContextsResource
isEncoded = false
paramClass = x.y.z.AccountService
paramType = x.y.z.AccountService
annotations = [@javax.annotation.Resource]

In this method and for this annotation, the parameter is created with a paramSource sets to Source.UNKNOWN. Why not.

A little later, jersey constructs the WADL document. On the WadlBuilder, wadl parameters are constructed like this for each fields:
Param wadlParam = generateParam(r, null, fp);

This methods returns null on parameters that are ENTITY or CONTEXT and lets the process continue for UNKNOWN ones:

private Param generateParam(AbstractResource r, AbstractMethod m, final Parameter p) {
        if (p.getSource() == Parameter.Source.ENTITY || p.getSource() == Parameter.Source.CONTEXT) {
            return null;
        }
        Param wadlParam = _wadlGenerator.createParam(r, m, p);
        return wadlParam;
    }

So the method createParam() is called with m = null. Unfortunately, in this method, m.getMethod() is called which leads to throw the exception.

Is it normal that the attributes of resources are parsed and especially involved in the construction process of Wadl documentation ? I really do not understand what is happening, especially since the library has been very stable so far...

Any help will be really appreciated because I'm short on solutions.

Thank you in advance

Chris