Monday, December 08, 2008

Nullable object must have a value

Ah, another modern classic error message from our friends at Microsoft. This one courtesy of the nullable type in C# 2.0 

This little gem pops up when you call the value method on a nullable, that has been set to null, which was the whole point of nullable in the first place. I get it, but why not just return null?

NullPointerException. As a java developer, the null pointer exception is one that you will see a lot. Sometimes by your own code errors sometimes not. I believe that when the original C# designers looked to make their java clone a better java, they looked to kill off the null pointer exception for good. This is why certain types like int and DateTime could not be set to null. Can't raise the exception if you can't set the value. Not necessarily a bad thing, but sometimes you just need a null. 

So in version 2 on, they give in and give us the nullable type, sorta. I say sorta because you can't get a null back out of it (via Value) or get a meaningful exception in return. So why do it at all?

No comments: