Null指针异常,同时将XML解组为具有@XMLElementWrapper的Java对象

时间:2014-03-28 21:49:54

标签: java xml jaxb

我正在使用JAXB将XML文件解组为Java Object。

此XML包含列表。 e.g

<items>
<item>
...
</item>
...
</items>

和Java Class为项目

设置了@XMLElementWrapper
@XMLElementWrapper(name="items")
@XMLElement(name="item")
private List<Item> items;

当我尝试解组时,我得到空指针异常。

 java.lang.NullPointerException
          at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:54)
          at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
          at java.lang.reflect.Field.get(Field.java:379)
          at com.sun.xml.bind.v2.runtime.reflect.Accessor$FieldReflection.get(Accessor.java:185)
          at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.startPacking(Lister.java:249)
          at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.startPacking(Lister.java:224)
          at com.sun.xml.bind.v2.runtime.unmarshaller.Scope.add(Scope.java:78)
          at com.sun.xml.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(ArrayERProperty.java:150)
          at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:404)
          at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:125)
          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 com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)
          at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
          at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
          at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
          at com.amazon.epubs.edition.manifest.serializer.JAXBMarshaller.unmarshall(JAXBMarshaller.java:45)
          at amazon.PeriodicalsManagementService.handler.PreviewPublicationHandler.getFolioRenditions(PreviewPublicationHandler.java:293)
          at amazon.PeriodicalsManagementService.handler.PreviewPublicationHandler.doMainProcessing(PreviewPublicationHandler.java:176)
          ... 19 more

1 个答案:

答案 0 :(得分:0)

尝试使用List<>的包装类:

@XmlRootElement
class Items {
    @XmlElement
    List<Item> item;
}

然后为unmarshall

执行Items