XML to Json using Jackson APi- NullPointer

371 views
Skip to first unread message

gauri mishra

unread,
May 24, 2016, 5:42:53 PM5/24/16
to jackson-user

 am trying to implement converting XML to JSON using the Jackson API and was following this as an example. XML deserialization to POJO using Jackson XmlMapper I have the exact same classes, xml file and the right imports but keep getting this in the stack trace:

Exception in thread "main" java.lang.NullPointerException
at com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser.addVirtualWrapping(FromXmlParser.java:279)
at com.fasterxml.jackson.dataformat.xml.deser.WrapperHandlingDeserializer._configureParser(WrapperHandlingDeserializer.java:141)
at com.fasterxml.jackson.dataformat.xml.deser.WrapperHandlingDeserializer.deserialize(WrapperHandlingDeserializer.java:109)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3807)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2691)
at Json1.main(Json1.java:25)

but when I change it to List.class instead of OpenCredentials.class it works. Basically my code does not work for any POJOs I built out myself.

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Json1 {
    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

        XmlMapper mapper = new XmlMapper();
        OpenCredentials openCredentials = mapper.readValue(new File("src/sample.xml"), OpenCredentials.class);
        System.out.println(openCredentials);
    }
}

And here are the POJOs I am using:

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

@JacksonXmlRootElement(localName = "open")
class OpenCredentials {

    @JacksonXmlProperty(localName = "creds")
    @JacksonXmlElementWrapper(useWrapping = false)
    private Credentials[] credentials;

    //getters, setters, toString
}

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

class Credentials {

    @JacksonXmlProperty(isAttribute = true)
    private String priv;

    @JacksonXmlProperty(isAttribute = true)
    private String type;

    private String user;

    @JacksonXmlProperty(localName = "client_token")
    private String clientToken;

    @JacksonXmlProperty(localName = "client_secret")
    private String clientSecret;

    //getters, setters, toString
}

Tatu Saloranta

unread,
May 24, 2016, 5:57:35 PM5/24/16
to jackso...@googlegroups.com
Which version of Jackson are you using? Make sure it is a relatively recent (ideally 2.7[.4]).
NullPointerException should never occur no matter what.

-+ Tatu +-

--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages