解组 JAXB 编组列表失败并出现 NullPointerException
Posted
技术标签:
【中文标题】解组 JAXB 编组列表失败并出现 NullPointerException【英文标题】:Unmarshall JAXB marshalled List failing with NullPointerExceptin 【发布时间】:2014-12-26 23:23:47 【问题描述】:我在 ListWrapper 中有一个 Person 对象列表,每个 Person 对象都包含一个电话号码列表。通过 JAXB.marschall(file, PersonList.class) 编组工作正常。问题是解组。我得到一个 NullPointerException,请参阅下面的堆栈跟踪。我怀疑注释丢失或错误,但我自己无法弄清楚......
代码
public class TelefonNr [..]
public class Person
[..]
List<TelefonNr> telefonNrs;
[..]
@XmlElement(name = "number")
@XmlElementWrapper(name = "telefonnumbers")
public List<TelefonNr> getTelefonNrs()
return this.telefonNrs;
[..]
@XmlRootElement(name = "persons")
public class PersonList
List<Person> persons;
[..]
@XmlElement(name = "person")
public List<Person> getPersons()
return this.Persons;
[..]
异常堆栈跟踪
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:289)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:253)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope.add(Scope.java:106)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(ArrayERProperty.java:198)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:523)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:143)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2973)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:648)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:175)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)
at org.twinhowi.organizer.core.DatastructureDemo.testUnmarshalling(DatastructureDemo.java:131)
at org.twinhowi.organizer.core.DatastructureDemo.main(DatastructureDemo.java:144)
【问题讨论】:
Help troubleshooting JAXB unmarshalling NPE 的可能重复项 这是一个简单的初始化问题,就像下面建议的 lexicore 【参考方案1】:理论:
您可能必须初始化集合 - 无论是急切的还是懒惰的。
List<TelefonNr> telefonNrs = new LinkedList<TelefonNr>();
或者:
public List<Person> getPersons()
if (this.persons == null)
this.persons = new LinkedList<Person>();
return this.persons;
但也请参阅这个问题:
Help troubleshooting JAXB unmarshalling NPE
【讨论】:
+1 - 如果没有相应的setPersons(List<Persion> persons)
方法,List
将需要按照您的说法进行初始化。以上是关于解组 JAXB 编组列表失败并出现 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章