This is what I do

March 4th, 2009 by Justin Leave a reply »

I’m going off the reservation tonight because I’m too tapped to blog about anything but tech.

Deal.

Slashdot discussion on the creation of null objects. The creator of the idea calls it a billion dollar mistake.

He’s probably right.

Somebody I’m currently working with asked for some pointers on getting up to speed in a limited area of VB.Net development. I spent about 15 minutes putting together a quick hit list of things that I figured they wouldn’t see coming. One of them was “Object reference not found” errors. You will see them. They will annoy you. You will spend time (and money) trying to figure out what went wrong. This was especially important because the environment we’re talking about (SSIS custom script tasks) doesn’t provide a debugger so you’re basically stuck with figuring it out with MsgBox() alerts.

The funny thing about the “problem” is we’re so used to dealing with null references that they’re just part of the way we think about problem solving. We rely on kicking back ‘null’ from some functions as a matter of practice. Quite often we expect it, but sometimes we don’t. Mostly in working with String types.

I don’t know how many times I’ve seen, or coded myself, is:if (!str.equals("")
{
// Do something
}
… and then seen it blow up when whatever portion of code was responsible for populating ‘str’ shoved a damned null into there. When it blows up you alter it:if (str != null && !str.equals(""))
{
// Do something
}
Problem solved, right? Yes. Unless you’re in VB.Net and forget that ‘And’ doesn’t sort circuit, part of the braindeadness that it inherited from VB.If (str IsNot Nothing AND Not str.Equals("")) Then
' do something
End
Code’s going to blow up. And doesn’t short circuit. What you wanted is:If (str IsNot Nothing AndAlso Not str.Equals("")) Then
' do something
End
Or, if you’re smart, and this is a little tip I picked up from an MIT grad with a phD, you make the language work for you a bit:if (! "".equals(str)
{
// Do something
}
Boom. You no longer need to worry about str being null because when it tries to construct a string out of it it’ll default to “” and everything works as intended. Unfortunately it doesn’t, and can’t, work the other way around because trying to deference a null reference is a Bad Thing and the VM has to blow up because anything else would be insanity.

It’s a weird issue. It’s not something that’s even remotely difficult to code around, but it crops up all the time. People. Just. Forget. What makes it worse is that when the issue really does manifest itself in the real world we’re usually dealing with something like:if (a.Equals("A1") && !b.Equals("B2") && c.Equals("C3")
{
// Do something
}
And all we get back at run time is “Object reference not set” or “NullPointerException” at line 18094. I understand why the VM can’t tell me which object is null, but that doesn’t make my job any easier. You still have to hunt it down and figure out what in the hell is going on.

I have no good answer to the issue, but I do find comfort in the fact that people are actually talking about it.

Advertisement

2 comments

  1. SPC says:

    Was going to call you showoff or asshole…couldn’t make up my mind …….

  2. Jeremy says:

    And I thought CNC g-code programming was mildly complicated……sheesh. I guess you can say you speak a second language.

Leave a Reply