我在ListWrapper中有一个Person对象列表,每个Person对象都包含一个telefonnummbers列表。通过JAXB.marschall(文件,PersonList.class)进行Marschalling工作正常。问题是解组。我得到一个NullPointerException,请参阅下面的stacktrace。我怀疑是否有错误或错误的注释,但我无法自己解决这个问题......
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)
答案 0 :(得分:7)
理论值:
您可能需要初始化集合 - 无论是渴望还是懒惰。
List<TelefonNr> telefonNrs = new LinkedList<TelefonNr>();
或者:
public List<Person> getPersons() {
if (this.persons == null) {
this.persons = new LinkedList<Person>();
}
return this.persons;
}
但也请看这个问题: