I have been provided an XSD file to unmarshall the XML returned from an external web service.
I’m able to unmarshall the XML I receive into the JAXB classes. But due to the depth of the structure of the JAXB classes generated I’m not sure on how to null check the code while retrieving values.
Example to retrieve a students name, the code is as follows –
jaxbResponse.getStudentDetails().get(0).getStudent().get(0).getName().get(0).getGivenName();
It gets really messy if I have to null check and index check the lists this code returns.
This is just an example, I need to get around 50 parameters with code similar to the above code. I have no idea when I’m going to get a NullPointerException or an IndexOutofBoundsException.
I’m new to working with JaxB classes, I need some help.
Thanks in advance.
The pattern of your example looks like a List generated from a simple
<xs:element minOccurs="{something}" maxOccurs="unbounded">in which case you should never get anullList (though you may get an empty list ifminOccursis 0), and you won’t getnullvalues out of the list either. You’ll only have to worry about nulls if you have an@XmlElementWrapperor your schema declares elements as nillable.I can’t be more specific without seeing (at least an extract from) your schema and/or generated classes.