Amazon deals

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

18 comments:

  1. 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.

    ReplyDelete
  2. 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...

    ReplyDelete
  3. 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...

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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

    ReplyDelete
  6. 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..

    ReplyDelete
  7. 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

    ReplyDelete
  8. Thank you very much, it save a lot of time!

    ReplyDelete
  9. nice post buddy... solved my problem too...

    ReplyDelete
  10. Thank you. It's a shame that this isn't handled more elegantly...

    ReplyDelete
  11. Thanks. You were my only good link off Google for this issue.

    ReplyDelete
  12. Exactly the problem I had. Saved me a hours work for sure. Thanks! ^.^

    ReplyDelete
  13. Thanks for your post! Helped me out a lot.

    ReplyDelete
  14. You saved my life, this would have taken so long without this post. Thanks a lot.

    ReplyDelete
  15. Dear Author.

    I am facing same problem, But as my requirement is that I have to pass the text node null. Because I have to create null xml tag like
    Please Let me know if any solution for it.

    Thanks

    ReplyDelete
  16. Thanks a lot! it's been bugging my code for days!

    ReplyDelete
  17. Hi,

    What if the error about "namespace that has not been declared"?Error javax.xml.transform TransformerException: java.lang.RuntimeException

    ReplyDelete