November 21, 2003

Checking for nulls..

I do not always nicely check for nulls (forgetting that or something). When I was debugging when something was null anyway (which should never happen in this case), I stumbled on something that I didn't know about java..
If you have this code snippet :

Test test = null;
test.setValue(new Test().getValue());

It will actually run the net Test().getValue() first and then throw a npe, because test is null.
This means : allways for nulls to save some useless time (if the parameter is conquer the world, it can take some time :) and even in some cases you want to have npe's thrown, check for null and if null throw a npe yourself.. At least no useless code is executed anymore.

This is probably known by everyone who read the java specs, but I am not much of a reader :) (I tend to fall asleep when reading too much :)

Posted by mvdb at November 21, 2003 01:23 PM
Comments
Post a comment