0

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>

Karson074
  • 127
  • 3
  • 14
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jens Oct 27 '19 at 17:46
  • That doesn't help me out. What I am trying to figure out is why Spring Batch is leaving the School Object values null instead of populating them. I think it may have something to do with the xmlns attribute in the SchoolList tag. – Karson074 Oct 27 '19 at 18:02
  • Have you tried setting the namespace in the fragment root element name like: `xmlFileReader.setFragmentRootElementName("{http://schemas.datacontract.org/2004/07/Diamond.Business.ThirdParty.School.Adapters.Prelude}School");` ? I see the declaration of your reader `StaxEventItemReader xmlFileReader` and not `StaxEventItemReader xmlFileReader`. Is that correct? It should be the same return type as the method signature. – Mahmoud Ben Hassine Oct 28 '19 at 10:57
  • The 'staxEventItemReader xmlFileReader' is correct. For this post I had changed some of the names around for security purposes. I had just missed that one when I did so. I'll fix that up now. – Karson074 Oct 28 '19 at 13:08
  • @Mahmoud Ben Hassine. I got the same exception when I tried your suggestion. – Karson074 Oct 28 '19 at 14:21

2 Answers2

0

Your XML is wrapped in SchoolList tags

So where is your SchoolList.java?

  • I shouldn't need a `SchoolList` object as the as the `xmlFileReader.setFragmentRootElementName("school");` tells the reader that the `School` tag is the root element that makes up each separate object. – Karson074 Oct 28 '19 at 14:23
0

This processor is looking for the "school" root element name. There are no such elements in your xml. Change this:

xmlFileReader.setFragmentRootElementName("school");

to this:

xmlFileReader.setFragmentRootElementName("School");
Michael Peacock
  • 2,011
  • 1
  • 11
  • 14