Java - JAXB XML unmarshal with optional fields
<p>I have issues with accessing fields of unmarshalled XML files, which contain optional tags. Here is a simple example I made up for a more complex case:</p>

<pre><code>&lt;people&gt;
    &lt;persons&gt;
        &lt;person&gt;
            &lt;id&gt;222&lt;/id&gt;
            &lt;pets&gt;
                &lt;pet&gt;
                    &lt;name age="2"&gt;Harry&lt;/name&gt;
                &lt;/pet&gt;
                &lt;pet&gt;
                    &lt;name&gt;Tiffany&lt;/name&gt;
                &lt;/pet&gt;
            &lt;/pets&gt;
        &lt;/person&gt;
        &lt;person&gt;
            &lt;id&gt;111&lt;/id&gt;
            &lt;pets&gt;
                &lt;pet value="1"&gt;&lt;/pet&gt;
            &lt;/pets&gt;
            &lt;spouse&gt;Frank&lt;/spouse&gt;
        &lt;/person&gt;
    &lt;/persons&gt;
&lt;/people&gt;
</code></pre>

<p>Notice that the second person has a spouse and the first does not. Additionally, the pets of the first person have names and the pets of the second person do not. The pet named Harry has also an age attribute. What I'm trying to show is that my XML files can have varying data, because of optional fields.</p>

<p>Here are my model classes for JAXB:</p>

<pre><code>@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class People {

    @XmlElementWrapper
    @XmlElement(name="person")
    private List&lt;Person&gt; persons;

    public List&lt;Person&gt; getPersons() {
        return persons;
    }

    public void setPersons(List&lt;Person&gt; persons) {
        this.persons= persons;
    }

}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {

    @XmlElement
    private int id;

    @XmlElementWrapper
    @XmlElement(name="pet")
    private List&lt;Pet&gt; pets;

    @XmlElement
    private String spouse;

    //getters and setters

}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class pet {

    @XmlAttribute
    private int age;

    @XmlValue
    private String name;

    //getters and setters

}
</code></pre>

<p>Now, let's say that I just want to print all names of pets. </p>

<pre><code>for (Person person : people.getPersons()) {
    for (Pet pet : person.getPets()) {
        System.out.println("Pet name: " + pet.getName());
    }
}
</code></pre>

<p>I'm getting a <code>NullPointerException</code>, if an inner tag is missing. Interestingly enough though, if you just search for a field in the people layer, it will just skip the XML tag as if it does not exist, like: </p>

<pre><code>for (Person person : people.getPersons()) {
    System.out.println("Spouse: " + person.getSpouse());
}
</code></pre>

<p>The above command works even though the first person does not have a spouse. It just provides the string "null", which works well for me. </p>

<p>A solution that I've tried - wrap each field in an <code>if</code> or <code>try-catch</code> statement (I prefer not to do this as there are hundreds of XML tags). Let me know if you have any suggestions. Thanks.</p>