Reputation: 10953
Please help me to find what's wrong withthis JAXB configuration.I am having this XML to parse.It throws Exception.
Excpetion :
group : DummyMaster@ae7b77
group : master
group : null
Exception in thread "main" java.lang.NullPointerException
at TestDummy.main(TestDummy.java:21)
XML :
<?xml version="1.0" ?>
<master name="master">
<detail name="detail1">
</detail>
<detail name="detail2">
</detail>
</master>
Test :
JAXBContext jaxbContext = JAXBContext.newInstance(DummyMaster.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
DummyMaster group = (DummyMaster) jaxbUnmarshaller.unmarshal(new File("test1.xml"));
System.out.println("group : "+group);
System.out.println("group : "+group.getName());
System.out.println("group : "+group.getDetails());
System.out.println("group : "+group.getDetails().size());//Line 21
Master :
@XmlRootElement(name="master")
public class DummyMaster {
private String name;
private List<DummyDetail> details;
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElementWrapper
@XmlElement
public List<DummyDetail> getDetails() {
return details;
}
public void setDetails(List<DummyDetail> details) {
this.details = details;
}
}
Detail :
@XmlRootElement(name="detail")
public class DummyDetail {
private String name;
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Upvotes: 1
Views: 248
Reputation: 3475
Choose any way as you required
Change only DummyMaster#getDetails()
(Without xml element wrapper)
//1. comment out XmlElementWrapper
//2. name with XmlElement
//@XmlElementWrapper
@XmlElement(name="detail")
public List<DummyDetail> getDetails() {
return details;
}
Change on DummyMaster#getDetails()
and xml file compose (With xml element wrapper)
//method
@XmlElementWrapper(name="details")
@XmlElement(name="detail")
public List<DummyDetail> getDetails() {
return details;
}
//in xml <details> should wrap child <detail> tag
<details>
<detail name="detail1">
</detail>
<detail name="detail2">
</detail>
</details>
Upvotes: 1
Reputation: 7894
You are receiving a NullPointerException on this line:
System.out.println("group : "+group.getDetails().size());
Given the lines above it are executed, the only possible explanation is that getDetails() is returning null, meaning when you attempt to call size() on this, you get an NPE.
Option 1
This is because your XML does not contain the details wrapper element.
Change your XML to:
<?xml version="1.0" ?>
<master name="master">
<details>
<detail name="detail1">
</detail>
<detail name="detail2">
</detail>
</details>
</master>
Option 2
In the DummyMaster class, remove the @XmlElementWrapper annotation completely and annotate the getDetails method with @XmlElement(name = "detail").
Then leave the XML as you originally had it.
Upvotes: 2
Reputation: 470
Change your DummyMaster class remove @XmlElementWrapper and add @XmlElement(name = "detail")
@XmlRootElement(name="master")
public class DummyMaster {
private String name;
private List<DummyDetail> details;
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "detail")
public List<DummyDetail> getDetails() {
return details;
}
public void setDetails(List<DummyDetail> details) {
this.details = details;
}
}
Upvotes: 2