Archive for the ‘Servlets and JSP’ Category

Null Pointer Error in XSLT Transformation

Monday, March 16th, 2009

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.

Courses in Java Programming Spring 2008

Tuesday, February 26th, 2008

I just received my assignment for the spring semester at Concordia University’s Continuing Education department. These are non-credit courses that focus on training students to write code in Java. I will be teaching:

CI651/C1 JAVA PROGRAMMING FUNDAMENTALS
Wednesdays 2008-04-02/2008-06-04
CI665/C1 JAVA SERVER-SIDE PROGRAMMING WITH SERVLETS AND JSP
Thursdays 2008-04-03/2008-06-05
CI675/C1 WEB SERVICES AND XML PROCESSING
Tuesdays 2008-04-01/2008-06-03

If you are in the Montreal region and looking to take courses in Java programming consider these courses. For more information on these courses and information on registering visit http://sarno.concordia.ca/conted/.