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

Sir
Am new to servlets.I am gettin null pointer exception.pls do reply.. i need a clear explanation for null point exception.why is it occuring.pls do reply.
Dear Barathi,
There is no specific criteria for the occurrence of a null pointer exception. It could be among a lot of reasons, few of them are listed below:
1. Referencing a variable that has been assigned null
2. Referencing an object before it has been instantiated
3. A null input case
4. Trying to instantiate a Bean that could only be interfaced with in case of EJBS
and many more…
hi iam getting same Error javax.xml.transform TransformerException: java.lang.NullPointerException because it ise getting a null value
at some textNode but all the text node value in my program are coming dynamically..i have handled all the nullPointers but still it is giving me the error please help if u can…
Since you are having dynamic text nodes, put a try block around the statements that could throw up the exception and then handle it in a catch block by allocating some value to the text node rather than retain its null value.
i had put a try block around the statements around text node’s for loop but the problem was the values were not null they were blank
so within try catch i put an if condition which was checking blank values it solved my problem…by the way thanks alot
Just had exactly the same problem, googled it and found your blog, solved it right away, thanks. Indeed, the “Error” error message is laughable, in a sad sad way..
Hello,
Like Pedro, I had this problem, googled for it, and arrived here. My specificity is the error did not come directly from my code, but from SPRING WS. Spring WS triggered this error while attempting to log the an incoming soap message. Disabling logging for spring ws solved my problem.
log4j.logger.org.springframework.ws=INFO
log4j.logger.org.springframework.xml=INFO