users@jaxb.java.net

Troubling error: NullPointerException at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:305)

From: Philippe Rolet <philippe.rolet_at_digimind.com>
Date: Wed, 18 May 2011 19:35:15 +0200

Dear JAXB users,
I am using JAXB to marshal/unmarshal some java classes and I recently came
accross an issue (details attached below).

I am trying to deserialize an XML file provided by a third party. The XML
validates against the schema that I generated from my java classes using
jaxb's schemagen tool without trouble. Then, when unmarshalling it, the
following exception occurs:

java.lang.NullPointerException
at
com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:305)
at
com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:269)
... (see below for full stack trace)

Since the XML validates against a schema generated automatically by
schemagen, I thought that no unmarshalling error could happen (I also
checked that all requiered classes are known to the JAXBContext). I tried to
search for the cause to this error on the net but I failed. I tried to see
what was happening in the source, and the only thing I grasped is that an
object was added to a "pack" field in Lister$CollectionLister that was not
initialized (hence the nullpointerexception).

Do you have any idea for the cause of this? is there an error in the XML
document that validation may have missed? did I miss something in the way I
unmarshal the object? Can you please help me solve the issue or find a
workaround?

Below are the functions that I wrote where the unmarshalling error happens,
and attached are the classes for which the problem occurs, the schema
generated from them, and the XML document that makes unmarshalling fail.

I am using JAXB jar artifact from the maven repository jaxb-impl-2.2.2.

Thank you very much for your help,

Best wishes,
Philippe Rolet

Code:

SemanticData res;
try {
String response=Tools.inputStreamToString(resp.getEntityInputStream());
Tools.validateXML(response,
applicationContext.getResource("classpath:"+outputSchema).getURL()); ==> XML
validation, no error
=> error here res=Tools.xmlToObject(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(applicationContext.getResource("classpath:"+outputSchema).getURL()),
response ,SemanticData.class);

} catch (JAXBException e) {
throw new ExtractorException("Could not convert XML response to object.",e);
               }

@SuppressWarnings("unchecked")
public static <T> T xmlToObject(Schema schema, String xmlString,Class<T>
objectClass) throws JAXBException {
JAXBContext context= JAXBContext.newInstance(objectClass);
Unmarshaller m=context.createUnmarshaller();
m.setSchema(schema);
=>error return (T)m.unmarshal(new StringReader(xmlString));
 }


Full stack trace:
java.lang.NullPointerException
at
com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:305)
at
com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:269)
at com.sun.xml.bind.v2.runtime.unmarshaller.Scope.add(Scope.java:121)
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:523)
at
com.sun.xml.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.endElement(ValidatingUnmarshaller.java:107)
at
com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:160)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:216)
at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:189)
at
javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at
javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
at com.digimind.wma.services.Tools.xmlToObject(Tools.java:99)
at
com.digimind.wma.services.extractor.ExtractorConnectorDDImpl.tagDocument(ExtractorConnectorDDImpl.java:68)
at
com.digimind.wma.services.test.ExtractorConnectorDDImplTest.testTagDocumentValidResult(ExtractorConnectorDDImplTest.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at
org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at
org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at
org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:44)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at
org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at
org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)