James Fox James Fox - 3 months ago 12
Java Question

Apache XmlBeans NullPointerException

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.

Answer

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.