TyC
TyC

Reputation: 792

JAXB - Checking to see if a child element is present

I think this question should be quick and easy for some of you more experience JAXB'ers. I simply need to check wether or not a child element is present as I'm unmarshalling a XML file.

Currently I'm getting the java.lang.NullPointerException error and I think it's because I'm trying to create an object (the child element) that doesn't exist. Here's a simple example.

<Meter>
  <MeterSessionInput>
    <MeterSessionOutput>
      <MeterSessionOutputOptical>

The MeterSessionOutputOptical child can be present, but doesn't have to be. I would like to check to see if the element exists before attempting to declare its object counter-part.

Upvotes: 1

Views: 3040

Answers (1)

nsfyn55
nsfyn55

Reputation: 15363

Your comment is the answer. Simply perform a null check on the child element with an if statement. You should be doing this anyway unless your using xsd and mandating that the subelement(s) element exists at binding time.

Upvotes: 3

Related Questions