我在执行解组时获得以下NullPointerException
:
ID and SortOrder----------------------------------
Exception in thread "main" java.lang.NullPointerException
at XmlToObject.main(XmlToObject.java:21)
您能告诉我代码错误的地方吗?我想我在这里执行了一些错误的注释工作。下面是我的代码文件。请帮忙。
的site.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Navigation>
<NavigationEntry id="3DAB2FFB-1F19-41C6-B38D-77A890DB4D40" sortOrder="1">
<hidepage>
a
</hidepage>
<name>
b
</name>
<url>
c
</url>
<entitlement>
d
</entitlement>
</NavigationEntry>
</Navigation>
Navigation.java
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Navigation")
public class Navigation {
private NavigationEntry NavigationEntry;
@XmlElement
public NavigationEntry getNavigationEntry() {
return NavigationEntry;
}
public void setNavigationEntry(NavigationEntry navigationEntry) {
NavigationEntry = navigationEntry;
}
}
NavigationEntry.java
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class NavigationEntry {
private String id;
private int sortOrder;
private String hidepage;
private String name;
private String url;
private String entitlement;
@XmlAttribute
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlAttribute
public int getSortOrder() {
return sortOrder;
}
public void setSortOrder(int sortOrder) {
this.sortOrder = sortOrder;
}
@XmlElement
public String getHidepage() {
return hidepage;
}
public void setHidepage(String hidepage) {
this.hidepage = hidepage;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@XmlElement
public String getEntitlement() {
return entitlement;
}
public void setEntitlement(String entitlement) {
this.entitlement = entitlement;
}
}
XmlToObject.java
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class XmlToObject {
public static void main(String[] args) {
try {
File file = new File("site.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Navigation.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Navigation que= (Navigation) jaxbUnmarshaller.unmarshal(file);
System.out.println("ID and SortOrder----------------------------------");
System.out.println(que.getNavigationEntry().getId() +" " + que.getNavigationEntry().getSortOrder());
System.out.println("Hidepage----------------------------------" + que.getNavigationEntry().getHidepage());
System.out.println("name----------------------------------" + que.getNavigationEntry().getName());
System.out.println("url----------------------------------" + que.getNavigationEntry().getUrl());
System.out.println("entitlement----------------------------------" + que.getNavigationEntry().getEntitlement());
System.out.println();
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
好。得到了答案。我没有在 Navigation.java
中添加 @XmlElement(name =“NavigationEntry”)问题已解决。 :)