1

I'm trying to unmarshall xml file, but get very strange NPE from the depths of JAXB library. Could you, please, help me to solve this problem?

Here's exception stacktrace's top:

java.lang.NullPointerException
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.handleGenericException(Loader.java:245)
at com.sun.xml.bind.v2.runtime.unmarshaller.Scope.add(Scope.java:123)
at com.sun.xml.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(ArrayERProperty.java:213)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:538)

Here's xml classes code:

public class Base {
    public Base() {
    }
}

public class A  extends Base {
    public A() {
    }
}

public class B extends Base{
    public B() {
    }
}


@XmlRootElement(name = "test")
@XmlAccessorType
public class TestXml {
    @XmlElementWrapper(name = "list")
    @XmlAnyElement
    @XmlJavaTypeAdapter(Adapter.class)
    public List<Base> list = new ArrayList<>();
}

Here's adapter's unmarshal() method, it was taken from here and was slightly modified.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public T unmarshal(Element element) throws Exception {
    if (null == element) {
        return null;
    }

    // 1. Determine the values type from the type attribute.
    Class<?> clazz = classLoader.loadClass(element.getAttribute("class"));

    // 2. Unmarshal the element based on the value's type.
    DOMSource source = new DOMSource(element);
    Unmarshaller unmarshaller = getJAXBContext(clazz).createUnmarshaller();
    JAXBElement jaxbElement = unmarshaller.unmarshal(source, clazz);

    return (T) jaxbElement.getValue();      
}

Here's test code:

JAXBContext jaxbContext = JAXBContext.newInstance(TestXml.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();       

TestXml auth = (TestXml) unmarshaller.unmarshal(new File("testData/test.xml"));

And finally, here's xml file, which I try to unmarshall:

<test>
    <list>
        <item class="my.package.A" />
        <item class="my.package.B" />
    </list>
</test>

While debugging I found out that adapter works well, i.e. unmarshall() method always returns instance of right class, but something bad happens after it.

The next thing I found out is that the adapter's code

JAXBElement jaxbElement = unmarshaller.unmarshal(source, clazz);

causes NPE. When I remove this line of code and replace unmarshall()'s return statement to

return new A(); //for example

no NPE occurs.

3 Answers 3

1

Try changing this line,

// 1. Determine the values type from the type attribute.
    Class<?> clazz = classLoader.loadClass(element.getAttribute("class"));

to,

// 1. Determine the values type from the type attribute.
    Class<?> clazz = ClassLoader.loadClass(element.getAttribute("class"));

Reason being your clazz variable is null.

Sign up to request clarification or add additional context in comments.

6 Comments

Aditya, ClassLoader class has no static method loadClass :-( I use Java 7.
Just check whether you are using java.lang.ClassLoader, coz ClassLoader in java7 has loadClass method. Check this link, docs.oracle.com/javase/7/docs/api/java/lang/…
Yes, it has, but this method is not static.
You can use Class.forName or refer this answer for loading your class using loadClass method, stackoverflow.com/a/18679222/2968614
classLoader finds class, so clazz variable is not null. Yes, and I get this classloader this way: classLoader = Thread.currentThread().getContextClassLoader();
|
1

I had the exact same stack trace when trying to process an unmarshalling process during another unmarshalling process.

Basically, in the 'unmarshal' method of an adapater, I built a fake 'tag' by stripping out its value and only keeping its attributes. Then, to extract the attributes properly, I used jaxb unmarshalling.

The error was very deep in jaxb and I really didn't see anything wrong in my code. I fixed my problem by migrating to latest & greatest jaxb-core library, from 2.2.6 to 2.2.11.

Comments

0

This is a bug in the version of JAXB (2.2.4-2) included with JDK 1.7.

For me, including the latest version of JAXB (2.2.11) in the classpath resolved this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.