Archive for March, 2009

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.

The Toyota Prius Experience

Monday, March 16th, 2009

My wife and I have been driving a 2008 Toyota Prius since December 2007. With a year under our belt I thought I would reflect on our experience in a way that might interest anyone considering a Prius. Our previous vehicle was a 2004 Honda CRV and prior to that we went through three different mini vans as our kids were growing up.

The Prius appealed to us on three levels. The first and most important was its gas mileage. Our CRV was averaging 14 litres per 100 kilometres which is about 17 miles per gallon. As gas prices were soaring to over $1.50 CDN per litre ($5.70 US a gallon) we calculated that the higher cost of a Prius would be offset by the lower operating costs. The Prius marketing untruth (every manufactures lies!) was 4.7 l/100Lm (50 mpg) on the highway and 4.2 l/100km (56 mpg) in the city.

The second thing that appealed to us was its emissions levels. I believe that we are contributing to global warming and a reduction is CO2 must be made by everyone. The Prius has the lowest emissions of any vehicle sold in North America.

And third was our vanity. I’m a computer geek and I look at the Prius as an example of extreme technology. If you will excuse the stretch in this analogy, the Prius is like the new US/NATO Joint Strike Force fighter aircraft. Neither would be able to move with the constant management of on-board computers. The Prius has four or five CPUs (without a GPS there is one less CPU) that are networked together to manage the dual engines and the planetary gear transmission. The Prius actually simulates the performance of a traditional automobile.

You can read many other articles on how the technology works, I certainly have. How it really works is what I have to say. The Prius is an ideal automobile when driven on smooth pavement when there is no possibility of ice, snow, or temperatures below freezing. Otherwise it requires nerves of steel and a resistance from trying to launch a class-action suit against Toyota.

Let’s start with air temperature. I live in Montreal, Canada where from December to March the temperature stays below freezing. The Prius supplies heat from its gasoline engine. The result is that the Prius gas engine almost never shuts off in the winter and the Toyota programming of its CPUs uses the gas engine to propel the car whenever the gas engine is running. The end result is that we average 8.7 l/100 km (27 mpg) in the city in the winter. I have already been approached by another Prius owner about launching a class-action suit based over this.

Next come ice and snow. After doing some reading about how the Prius works, I learned that the torque of the electric motors is strong enough to shear the front wheels off the car if they were allowed to spin on ice or snow (or even sand). To prevent this from happening and to protect the regenerative recharging system the Prius uses a traction control system that cuts the power to the front wheels if either of them begin to spin even the slightest amount. The wheels come to a complete stop regardless of how hard you are pressing on the gas pedal. Now I have been told that many modern cars use a similar system because it prevents the car from sliding sideways of slippery ground. That it does but on a number of occasions I have found myself at a dead stop completely unable to move forward both on hills and even level ground.  In each of these situations I needed to back up (traction control does not seem to engage when backing up) and take a run at the slippery spot.

Finally there is uneven pavement. When you step on the brakes on a Prius I have been told that the disc brakes DO NOT engage. Instead the computers use the resistance in the electric motor to slow the vehicle. Only when you expect to come to a full stop will the physical brakes engage. This means that a Prius will only need to have its brakes changed every 100,000 km (62500 miles). This technique is an example of what I mean by saying the Prius simulates a conventional automobile.

The problem occurs when stopping on uneven pavement as is found in any city that has winter. Freezing and thawing destroys asphalt and creates uneven pavement, cracks, and potholes. When braking on pavement damaged this way the Prius detects that the two front wheels are no longer spinning at the same speed. Regardless of the cause the Prius reacts by shutting down power to the wheels. In a braking situation this means that the electric motor’s resistance is disconnected and the physical braking system must be engaged. There is a delay of about a second between when the electric motor disengages and the disc brakes engage. The feeling is as if the car is suddenly accelerating. While the car is not accelerating, that brief moment of complete loss of braking scared the hell out us when we first encountered it. We have learned to ignore it or if it is a real panic situation then you must bang on the brake pedal which overrides the computer and engages the disc brake immediately.

I still have three years left in my lease and I do not plan to find a way to get out of it. It still gets better gas consumption that our previous cars although I am pissed that Toyota is not forthcoming on the issue of winter driving. Driving on ice and snow is a problem many people face with late model two-wheel drive cars and I have trained myself to deal with the braking issue.

When the time comes to replace the Prius my wife and I have come to the conclusion that in our climate only a four wheel drive vehicle makes sense. Hopefully come December 2011 there will be a RAV4 or CRV hybrid. I forget to mention that we actually wanted the Ford Escape Hybrid but Ford wanted 19% for lease!