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

Xforms submission giving null parameters to request.getparameter() - Solution

Simplicity side of X-forms and xml apart, when one goes building any major web app from the very little, itsy bitsy knowledge we have, looms large problems that shake the very fundamentals of what you began coding with. If you ever tried the previous Xform in a debug mode on the Websphere server you would have found out now the hard way that the form does pass values, but jsp page cannot recognise in whatsoever.  Its an irksome problem i had last night and spent around 6 hours of my valuble time figuring out, going through over 1000 links on google and reading over 10 pdfs, to know there isn'at a solution posted anywhere. Ok fine leave apart my difficulties, here's what you came for, the solution to the problem

 

Goto your submission element

<xf:submission action=”process.jsp”
method=”get” id=”sub”/>


add an extra parameter

<xf:submission action=”process.jsp”
method=”get”
seperator="&amp;" id=”sub”/>

voila, you have your parameters now.

Now, the exact reason why the problem occured:

An html form uses the seperator by default and a jsp seperates out and creates a parameter map from this. Xforms by default, doesn't specify the standard seperator, so the addition tag required in the submission element in your model.