I recently stumbled on NullPointerException in Transformer.transform (Source, Result) method, my application was working fine until yesterday, there was no update, no change in configuration of anything…
What I was doing there is a textbook example of usage of Transformer class:
Source source = new DOMSource (doc); Result result = new StreamResult (new File (file)); Transformer transformer = TransformerFactory.newInstance ().newTransformer (); transformer.transform (source, result);
Transformer is a part of JVM and it’s used mostly for saving Document to file, so first thoughts were to check the parameters passed. Yes, there was a valid path inside File, canWrite () returned true, the file was created indeed (albeit having zero size) but that was all. The only message I got came to stderr and read:
ERROR: ''
Very helpful, isn’t it? What next? I went to check the stacktrace:
javax.xml.transform.TransformerException: java.lang.NullPointerException
at com.sun.org.apache.xalan.internal.xsltc.trax. TransformerImpl.transform(TransformerImpl.java:651)
at com.sun.org.apache.xalan.internal.xsltc.trax. TransformerImpl.transform(TransformerImpl.java:281)
at xxx.Main.writeDocumentToFile(Main.java:151)
at xxx.Main.main(Main.java:332)
Caused by: java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer. ToUnknownStream.characters(ToUnknownStream.java:312)
at com.sun.org.apache.xalan.internal.xsltc.trax. DOM2TO.parse(DOM2TO.java:229)
at com.sun.org.apache.xalan.internal.xsltc.trax. DOM2TO.parse(DOM2TO.java:215)
at com.sun.org.apache.xalan.internal.xsltc.trax. DOM2TO.parse(DOM2TO.java:215)
at com.sun.org.apache.xalan.internal.xsltc.trax. DOM2TO.parse(DOM2TO.java:215)
at com.sun.org.apache.xalan.internal.xsltc.trax. DOM2TO.parse(DOM2TO.java:121)
at com.sun.org.apache.xalan.internal.xsltc.trax. DOM2TO.parse(DOM2TO.java:85)
at com.sun.org.apache.xalan.internal.xsltc.trax. TransformerImpl.transformIdentity(TransformerImpl.java:596)
at com.sun.org.apache.xalan.internal.xsltc.trax. TransformerImpl.transform(TransformerImpl.java:642)
... 3 more
---------
java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer. ToUnknownStream.characters(ToUnknownStream.java:312)
at com.sun.org.apache.xalan.internal.xsltc.trax. DOM2TO.parse(DOM2TO.java:229)
at com.sun.org.apache.xalan.internal.xsltc.trax. DOM2TO.parse(DOM2TO.java:215)
at com.sun.org.apache.xalan.internal.xsltc.trax. DOM2TO.parse(DOM2TO.java:215)
at com.sun.org.apache.xalan.internal.xsltc.trax. DOM2TO.parse(DOM2TO.java:215)
at com.sun.org.apache.xalan.internal.xsltc.trax. DOM2TO.parse(DOM2TO.java:121)
at com.sun.org.apache.xalan.internal.xsltc.trax. DOM2TO.parse(DOM2TO.java:85)
at com.sun.org.apache.xalan.internal.xsltc.trax. TransformerImpl.transformIdentity(TransformerImpl.java:596)
at com.sun.org.apache.xalan.internal.xsltc.trax. TransformerImpl.transform(TransformerImpl.java:642)
at com.sun.org.apache.xalan.internal.xsltc.trax. TransformerImpl.transform(TransformerImpl.java:281)
at imdbparser.Main.writeDocumentToFile(Main.java:151)
at imdbparser.Main.main(Main.java:332)
Fortunately I’m not afraid of going deep into the code, so I grabbed sources and checked the line where everything got started:
Caused by: java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer. ToUnknownStream.characters(ToUnknownStream.java:312)
the code there looks like this:
public void characters(String chars) throws SAXException { final int length = chars.length(); (...)
Gotcha! The only possible cause of that is chars
being null
, fine but… why? Previous line in stacktrace finally helped to resolve the problem:
case Node.TEXT_NODE: _handler.characters(node.getNodeValue()); break;
So there’s one or more #TEXT nodes in the document having null value! It was quite easy to find the problem having this information, it was on following ternary expression:
m != null ? m.name : getMName (files[i].getName ())
At first sight it looks good – there is a test on null
value but what if m
is not null
and m.name
is?
The Owls Are Not (always) What They Seem (Twin Peaks)