To get suggestion for the cause of this issue and solution/workaround.
NullPointerException when running almost the same program in Stackoverflow Split 1GB Xml file using Java to split the XML file having xmlns="" as in W3C specification.
Validated with https://validator.w3.org/check and confirmed well-formed.
<?xml version="1.0" encoding="UTF-8"?>
<EntityList>
<Entity xmlns="">
</Entity>
<Entity xmlns="">
</Entity>
</EntityList>
JAXP: find factoryId =javax.xml.stream.XMLInputFactory
JAXP: loaded from fallback value: com.sun.xml.internal.stream.XMLInputFactoryImpl
JAXP: created new instance of class com.sun.xml.internal.stream.XMLInputFactoryImpl using ClassLoader: null
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: loaded from fallback value: com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl using ClassLoader: null
ERROR: ''
Exception in thread "main" javax.xml.transform.TransformerException: java.lang.NullPointerException
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at stax.cursor.Demo.main(Demo.java:24)
Caused by: java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.NamespaceMappings.pushNamespace(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToStream.startPrefixMapping(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.emitFirstTag(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.flush(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.startElement(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleStartElement(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
... 3 more
---------
java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.NamespaceMappings.pushNamespace(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToStream.startPrefixMapping(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.emitFirstTag(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.flush(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.startElement(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleStartElement(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at stax.cursor.Demo.main(Demo.java:24)
Tested with Java 7 and 8 on Windows 10 Pro 64 bit on DELL latitude E7440
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)
package stax.cursor;
import java.io.File;
import java.io.FileReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
xsr.nextTag(); // Advance to statements element
long count = 1;
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT && xsr.getLocalName().toString().equals("Entity")) {
File file = new File("out/" + java.lang.Long.valueOf(count++) + ".xml");
t.transform(new StAXSource(xsr), new StreamResult(file));
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<EntityList>
<Entity xmlns="mailto:test.com">
</Entity>
<Entity>
</Entity>
</EntityList>
This XML is split into two XML files as expected.
1.xml
<?xml version="1.0" encoding="UTF-8"?><Entity xmlns="mailto:test.com">
</Entity>
2.xml
<?xml version="1.0" encoding="UTF-8"?><Entity>
</Entity>
This is a bug in the JDK. You should file a bug report at http://bugs.java.com/
As workaround you could try to use an alternative Stax implementation.