James Fox James Fox - 11 months ago 44
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">

My code is:

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

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
} catch (XmlException e) {
// TODO Auto-generated catch block

above results in a NullPointerException and this seems to point to either the
method within the XMLBeans classes or an issue with the namespaces.

I have tried using a substitute namespace map and have toggled the
method on and off (it is currently not commented out above). I have also read an answer regarding adding
to the
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.


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.