users@jersey.java.net

NullPointerException reading empty JSON array

From: Chad McHenry <mchenryc_at_gmail.com>
Date: Fri, 29 May 2009 11:49:37 -0400

I'm getting an NPE when part of my put data has an empty array. I
intended it to unmarshal to a List<Integer> (and I've tried with a
List of more complex objects). Is there a way to get it to unmarshal
an empty array into an empty List?

@Path("/foo")
@Produces(MediaType.APPLICATION_JSON)
@Component
@Scope("singleton")
public class MediaResource {
    @POST
    public Response create(Foo foo) {
        ...
    }


@XmlRootElement
public class Foo {
    public List<Integer> barList;
}

curl -X POST -d '{"barList":[]}' -H 'Content-type: application/json'
http://localhost/myapp/foo

java.lang.NullPointerException
        at com.sun.jersey.json.impl.reader.Jackson2StaxReader.readNext(Jackson2StaxReader.java:179)
        at com.sun.jersey.json.impl.reader.Jackson2StaxReader.readNext(Jackson2StaxReader.java:106)
        at com.sun.jersey.json.impl.reader.Jackson2StaxReader.next(Jackson2StaxReader.java:232)
        at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:192)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:360)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:339)
        at com.sun.jersey.json.impl.JSONUnmarshaller.unmarshal(JSONUnmarshaller.java:100)
        at com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider.readFrom(JSONRootElementProvider.java:104)
        at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:97)
        at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:391)
        at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:81)
        at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityParamInInvoker.getParams(EntityParamDispatchProvider.java:99)
        at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$TypeOutInvoker._dispatch(EntityParamDispatchProvider.java:136)
        at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
        at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:154)
        at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
        at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:71)
        at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
        at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:63)
        at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:543)
        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:502)
        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:493)
        at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:308)
        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:314)
        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:239)