gpt4 book ai didi

java - 将 XML 解码到具有 @XMLElementWrapper 的 Java 对象时出现空指针异常

转载 作者:太空宇宙 更新时间:2023-11-04 14:59:45 24 4
gpt4 key购买 nike

我正在使用 JAXB 将 XML 文件解码为 Java 对象。

此 XML 包含列表。例如

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

Java 类有 @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

最佳答案

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

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

然后执行unmarshall对于Items

关于java - 将 XML 解码到具有 @XMLElementWrapper 的 Java 对象时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22723778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com