Null Pointer Error in XSLT Transformation

In a recent lab session with my students one of them presented me with what I thought would be a simple problem to resolve. Their code was transforming a DOM into an XML document using the XSLT engine in Java 1.6. The source of the data was an ArrayList of JavaBeans created from a JDBC RecordSet. Creating the DOM went smoothly but when the transformation was called on a null pointer exception occurred. The stack trace pointed to a line of code that read:

transformer.transform(xmlSource, result);

So as I usually do I had the student add some System.out.println statements to determine which reference was null. To my surprise every reference was not null.

Next I had the student print out the values from the ArrayList’s JavaBeans as they were being entered into the DOM. We saw two things. First, one of the five special characters that must be an entity in an XML document was coming out of the beans. Second was the fact that some of the strings were null.

So I made a wrong assumption and then failed to test my assumption. I assumed that the problem was the special characters and so directed the students to create a method to replace the characters with their entities (i.e. & becomes &). The next day a student came to see me and suggested that the problem was really the null strings. So I finally got around to doing the tests.

What I learned is that the transformation of a special character in the DOM to an XML was being done as part of the transformation. There was no need to write a method to convert the special characters to entities on the way into the DOM. Doing what I said resulted in the characters that made up an entity remaining. For example:

Original string: cats & dogs

Converted string: cats & dogs

The converted string went into the DOM and then appeared in the XML as:

<housepets>cats &amp;amp; dogs</housepets>

Certainly not what is wanted so converting characters before they go into the DOM was not the solution and therefore special characters were not the problem.

The real problem is the null strings. While the DOM was happy to reference a null string, the transformation engine sees the string for what it really is, a null pointer.

The solution is to either check for null strings when building the DOM or to have the set methods in the beans recognize a null string and create an empty string in its place.

Problem solved and another successful demonstration that the teacher is human.

Email this to someoneTweet about this on TwitterShare on LinkedIn0Share on Facebook0

About Ken

I am the Program Coordinator and Chairperson of the Computer Science Technology program at Dawson College in Montreal, Canada. I am also a Program Consultant to and part-time instructor in the Computer Institute of Concordia University's School of Extended Learning.

2 thoughts on “Null Pointer Error in XSLT Transformation

  1. John Salchichon

    Sir, you saved my day and maybe many others more. Just by changing one return in my project, which by default returns a null, to a empty string everything was fixed.

Leave a Reply

Your email address will not be published. Required fields are marked *