我正在使用一个我无法控制的API,它会返回XML。
基本上我有directory
个对象,它可以包含多个directory
和file
个对象,这些对象没有被任何标记包裹,在少数其他原始字段中。
file
对象包含少量原始字段,以及由tPathList
和oPathList
标记包装的2个列表。
以下是此类XML的示例:
<hwreply>
<result>1</result>
<directory>
<file>
<created>DATE</created>
<modified>DATE</modified>
<name>STRING</name>
<size>INT</size>
<tPath>STRING</tPath>
<oPath>STRING</oPath>
<aPath>STRING</aPath>
<tPathList>
<tPath>STRING</tPath>
...
</tPathList>
<oPathList>
<oPath>STRING</oPath>
...
</oPathList>
</file>
<file>...</file>
...
<directory>...</directory>
<directory>...</directory>
...
<nEntries>5</nEntries>
<created>DATE</created>
<modified>DATE</modified>
</directory>
</hwreply>
我创建了目录和文件对象,以及作为根的OpenDirectory。当我打电话
OpenDirectory od = response.getEntity(OpenDirectory.class);
我得到以下异常:
Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:290)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:254)
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:195)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:507)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:145)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
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:2938)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:120)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:103)
at com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider.readFrom(XMLRootElementProvider.java:115)
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:111)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:553)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:506)
at liveperson.lphosting.plugins.cdn.proxy.highwinds.HighwindsProxy.getDirectory(HighwindsProxy.java:49)
at liveperson.lphosting.plugins.cdn.proxy.highwinds.HighwindsProxy.main(HighwindsProxy.java:59)
我认为它与我所拥有的其中一个列表有关,但我无法确定哪里出错了。任何帮助将不胜感激。
提前致谢。
以下是课程(减去几个字段/方法):
@XmlRootElement(name = "hwreply")
public class OpenDirectory extends ResponseBase {
@XmlElement(name="session")
public Session getSession() {...}
public void setSession(Session session) {...}
@XmlElement(name="directory")
public Directory getDirectory() {...}
public void setDirectory(Directory directory) {...}
}
public class Directory {
...
private List<Directory> directories;
private List<File> files;
@XmlElement(name="nEntries")
public int getnEntries() {...}
public void setnEntries(int nEntries) {...}
@XmlElement(name="name")
public String getName() {... }
public void setName(String name) {... }
@XmlElement(name="readonly")
public boolean isReadonly() {... }
public void setReadonly(boolean readonly) { ... }
@XmlElement (name="created")
public String getCreated() { ... }
public void setCreated(String created) { ... }
@XmlElement(name="modified")
public String getModified() {... }
public void setModified(String modified) {... }
@XmlElements(
@XmlElement(name="directory", type=Directory.class)
)
public List<Directory> getDirectories() {
return directories;
}
public void setDirectories(List directories) {
this.directories = directories;
}
@XmlElements(
@XmlElement(name="file", type=File.class)
)
public List<File> getFiles() {
return files;
}
public void setFiles(List files) {
this.files = files;
}
}
public class File {
private List<String> tPathList;
private List<String> oPathList;
@XmlElement(name="xferStatus")
public int getXferStatus() {...}
public void setXferStatus(int xferStatus) {...}
@XmlElement(name="size")
public int getSize() {...}
public void setSize(int size) {...}
@XmlElement(name="tPath")
public String gettPath() {...}
public void settPath(String tPath) {...}
@XmlElement(name="oPath")
public String getoPath() {...}
public void setoPath(String oPath) {...}
@XmlElementWrapper(name="tPathList")
@XmlElements(
@XmlElement(name="tPath", type=String.class)
)
public List gettPathList() {
return tPathList;
}
public void settPathList(List tPathList) {...}
@XmlElementWrapper(name="oPathList")
@XmlElements(
@XmlElement(name="oPath", type=String.class)
)
public List getoPathList() {
return oPathList;
}
public void setoPathList(List oPathList) {
this.oPathList = oPathList;
}
}
答案 0 :(得分:6)
问题由OP自己解决,并将其作为答案添加。
发现问题。如果它可以帮助任何人:
File类中的 setFiles(List files)
应为setFiles(List<File> files)
。
答案 1 :(得分:1)
我也有同样的例外。但解决方案不同。我在这里分享,为未来的问题解决者。调用JAXBContext.newInstance()
时,请查看返回的对象。
通常它应该是com.sun.xml.bind.v2.runtime.JAXBContextImpl
类型。但是,如果它来自一些glassfish3库,如:
jar:file.../glassfish3/glassfish/modules/jaxb-osgi.jar
给了我同样的例外。改变了类路径的顺序,如果JAXBContext.newInstance()
从这个jar中找到第一个实现JAXBContext
类,最后解组工作正常:
jar:file:.../.m2/repository/com/sun/xml/bind/jaxb-impl/2.x.x/jaxb-impl-2.x.x.jar