Amazon deals

Showing posts with label SAX. Show all posts
Showing posts with label SAX. Show all posts

Wednesday, October 22, 2008

javax.xml.transform TransformerException: java.lang.NullPointerException : How to Solve?

Just after solving my previous problem I 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 therewas a simple attempt to use the transformer from the xml api to write an xml file:

Source source = new DOMSource (doc);
Result result = new StreamResult (new File (file));
Transformer transformer = TransformerFactory.newInstance ().newTransformer ();
transformer.transform (source, result);


Personally this is the worlds's most stupid error. Guess what the console gave as the eroor description : ERROR: '' "

Very helpful, isn’t it? What next? Well we'll proceed to the stack trace next:

Down somewhere in the jungle of exceptions you get 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();


Now we finally see the part that threw the error. Well the char.kength() was zero because there was a null text node insertion beging tried. So now's the solution to this problem

1. Go through your source code

2. Find the text node that is null

3. Give it a String value

4. Recompile and run

OWLS ARE NOT ALWAYS WHAT THEY SEEM