I am trying to read an xml file using Spring Batch so I can parse out certain data and place it in a dat file. However whenever I try to process the file I get a NullPointerException. Investigating the reason, it seems that the object I am trying to populate values are being set to null. It creates a list of the same amount of School tags found in the xml but the values are null. Below is an example of the xml I am trying to read and the code I am using to populate the object. What am I doing wrong?
ItemReader
@Bean
ItemReader<school> underwritingXmlFileItemReader(){ //Environment environment
StaxEventItemReader<School> xmlFileReader = new StaxEventItemReader<>();
xmlFileReader.setResource(editedInput);
xmlFileReader.setFragmentRootElementName("school");
Jaxb2Marshaller schoolMarshaller = new Jaxb2Marshaller();
schoolMarshaller.setClassesToBeBound(School.class);
xmlFileReader.setUnmarshaller(schoolMarshaller);
return xmlFileReader;
}
School.class
@XmlRootElement(name="School", namespace="http://schemas.datacontract.org/2004/07/Diamond.Business.ThirdParty.School.Adapters.Prelude")
@XmlAccessorType(XmlAccessType.NONE)
public class School {
@XmlElement(name="SchoolNumber", defaultValue="")
private String uniqueId;
@XmlElement(name="Teacher", defaultValue= "")
private Teacher teacher;
public String getUniqueId() {
return uniqueId;
}
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
public Policyholder getPolicyholder() {
return policyholder;
}
public void setTeacher(Teacher teacher) {
this.policyholder = policyHolder;
}
}
Teacher.class
@XmlRootElement(name="Teacher", namespace="http://schemas.datacontract.org/2004/07/Diamond.Business.ThirdParty.School.Adapters.Prelude")
@XmlAccessorType(XmlAccessType.NONE)
public class Teacher {
@XmlElement(name = "DisplayName", defaultValue="")
private String fullName;
@XmlElement(name = "DOB", defaultValue="")
private String dob;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
}
SchoolList.xml
<SchoolList xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Diamond.Business.ThirdParty.School.Adapters.Prelude">
<School>
<Teacher>
<DOB>1/1/1970</DOB>
<DisplayName>Ziva Brown</DisplayName>
<DoingBusinessAs />
<FirstName>Ziva</FirstName>
<LastName>Brown</LastName>
<MiddleName />
</Teacher>
</School>
<School>
<Teacher>
<DOB>1/1/1970</DOB>
<DisplayName>Alex John</DisplayName>
<DoingBusinessAs />
<FirstName>Alex</FirstName>
<LastName>John</LastName>
<MiddleName />
</Teacher>
</School>
</SchoolList>