Apache XmlBeans NullPointerException
Asked Answered
R

1

9

I am trying to parse a pacs.003 ISO20022 formatted xml file. I have the XSD for this and using XMLBeans have created the required Java classes. The problem I am having is that I am not able to read an element from the XML and keep getting a NullPointerException. I have searched for similar problems but most result in the OP moving to a different technology.

The XML snippet I have from LON_20160208.xml is:

  <S2SDDDnf:FIToFICstmrDrctDbt xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.003.001.02">
    <GrpHdr>
      <MsgId>DDA160802AASW006543</MsgId>
    </GrpHdr>
  </S2SDDDnf:FIToFICstmrDrctDbt>

My code is:

  public static void main(String[] args) {
    XmlOptions xmlOptions = new XmlOptions();
    xmlOptions.setUseDefaultNamespace();
    xmlOptions.setSavePrettyPrint();

    Document doc;

    try {
        doc = Document.Factory.parse(new File("data_samples/LON_20160208.xml"));
        String messageId = doc.getFIToFICstmrDrctDbt().getGrpHdr().getMsgId();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XmlException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

The doc.getFIToFICstmrDrctDbt() above results in a NullPointerException and this seems to point to either the get_store() method within the XMLBeans classes or an issue with the namespaces.

I have tried using a substitute namespace map and have toggled the setUseDefaultNamespace() method on and off (it is currently not commented out above). I have also read an answer regarding adding elementFormDefault="qualified" to the xsd:schema element but this has already been done. None of these seem to fix the issue and I am running out of ideas.

Any help would be greatly appreciated.

Romansh answered 2/8, 2016 at 16:2 Comment(5)
Is data_samples/LON_20160208.xml reachable?Slipperwort
Yep I am able to read the XML fine and the doc variable is not null.Romansh
try removing S2SDDDnf: from your doc element and then read it. if it can read the there might be problem with generated classesSlipperwort
This doesn't fix the problem. I can load substitute namespaces but this hasn't fixed it before either.Romansh
Please add stacktrace.Merca
R
6

I have been able to fix the issue. It was due to the fact that the message is a more specific SEPA DNF file which is not a generic pacs.003 file. This means it needed a different schema.

The null messages were because the xml does not have a <Document> root tag, it is a <MPEDDDnfBlkDirDeb> root tag.

Romansh answered 9/8, 2016 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.