简体   繁体   中英

JAXB Simple XML Parsing

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;
}
}

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.

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;
    }
}

Choose any way as you required

  1. 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; } 
  2. 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM