Alex Rivera | Logout

Why is "null" present in C# and Java?

Asked 2008-10-07T11:30:36.560
76

We noticed that lots of bugs in our software developed in C# (or Java) cause a NullReferenceException.

Is there a reason why "null" has even been included in the language?

After all, if there were no "null", I would have no bug, right?

In other words, what feature in the language couldn't work without null?

Edit
Report

1 Answer

0
  • Explicit nulling, though that is seldom necessary. Perhaps one can view it as a form of defensive programming.
  • Use it (or nullable(T) structure for non-nullables) as a flag to indicate a missing field when mapping fields from one data source (byte stream, database table) to an object. It can get very unwieldy to make a boolean flag for every possible nullable field, and it may be impossible to use sentinel values like -1 or 0 when all values in the range that field is valid. This is especially handy when there are many many fields.

Whether these are cases of use or abuse is subjective, but I use them sometimes.

answered 2010-11-09T03:34:30.360

Your Answer