java - Unmarshal NullPointerException (with JAXB reference implementation) -
i'm trying unmarshall xml file, strange npe depths of jaxb library. you, please, me solve problem?
here's exception stacktrace's top:
java.lang.nullpointerexception @ com.sun.xml.bind.v2.runtime.unmarshaller.loader.reporterror(loader.java:258) @ com.sun.xml.bind.v2.runtime.unmarshaller.loader.handlegenericexception(loader.java:245) @ com.sun.xml.bind.v2.runtime.unmarshaller.scope.add(scope.java:123) @ com.sun.xml.bind.v2.runtime.property.arrayerproperty$receiverimpl.receive(arrayerproperty.java:213) @ com.sun.xml.bind.v2.runtime.unmarshaller.unmarshallingcontext.endelement(unmarshallingcontext.java:538)
here's xml classes code:
public class base { public base() { } } public class 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, taken here , modified.
@suppresswarnings({ "unchecked", "rawtypes" }) @override public t unmarshal(element element) throws exception { if (null == element) { return null; } // 1. determine values type type attribute. class<?> clazz = classloader.loadclass(element.getattribute("class")); // 2. unmarshal element based on 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, try unmarshall:
<test> <list> <item class="my.package.a" /> <item class="my.package.b" /> </list> </test>
while debugging found out adapter works well, i.e. unmarshall() method returns instance of right class, bad happens after it.
the next thing found out adapter's code
jaxbelement jaxbelement = unmarshaller.unmarshal(source, clazz);
causes npe. when remove line of code , replace unmarshall()'s return statement
return new a(); //for example
no npe occurs.
try changing line,
// 1. determine values type type attribute. class<?> clazz = classloader.loadclass(element.getattribute("class"));
to,
// 1. determine values type type attribute. class<?> clazz = classloader.loadclass(element.getattribute("class"));
reason being clazz variable null.
Comments
Post a Comment