使用 Jaxb API 将 XML 解组为 Java 对象时获取 NullPointerException
Posted
技术标签:
【中文标题】使用 Jaxb API 将 XML 解组为 Java 对象时获取 NullPointerException【英文标题】:Getting NullPointerException while unmarshalling the XML to Java Object using Jaxb API 【发布时间】:2015-12-15 15:42:17 【问题描述】:我在执行解组时收到以下NullPointerException
:
ID and SortOrder----------------------------------
Exception in thread "main" java.lang.NullPointerException
at XmlToObject.main(XmlToObject.java:21)
能否请您告诉我我的代码哪里出错了。我想我在这里执行了一些错误的注释工作。下面是我的代码文件。请帮忙。
网站.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>
导航.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();
【问题讨论】:
【参考方案1】:嗯。得到了答案。我没有在 Navigation.java
中添加 @XmlElement(name="NavigationEntry")问题已解决。 :)
【讨论】:
以上是关于使用 Jaxb API 将 XML 解组为 Java 对象时获取 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 jaxb 将 xml 字符串解组为 java 对象
在将 XML 文件解组为对象后,如何让 JAXB 调用方法?