Handling null in .NET

One of the trickier aspects of application development is dealing with null or nonexistent data. Tony Patton takes a closer look at null values in the .NET framework.
Written by Tony Patton, Contributor on
Null or nonexistent data gets complicated when the application expects a data value and gets nothing or unexpected values; that's when you must perform coding that properly handles these situations to avoid application failure. This requires a close examination of the data wherever and whenever it's needed.

Null and void
A null value is a value that doesn't refer to any object. It's the default value of reference-type variables (e.g., class, interface, delegate).

These items are called reference types because they're objects that store references to the actual data. String, object, and struct are reference type examples. The null keyword is a literal that represents a null reference with the C# .NET environment. You may use the null keyword to check or assign the value of an object. For example, the following C# code determines if a string value is null:

string sTest = "Test";
if (sTest == null) {
Console.WriteLine("sTest is Null")

This code works without any problems with C#, but there's no VB.NET equivalent of the null keyword. Instead, VB.NET uses the Nothing keyword. The following code demonstrates its use:

Dim sTest As String
If (sTest Is Nothing) Then
Console.WriteLine("sTest is Null")
End If

Another area of confusion stems from the fact that VB.NET doesn't treat null and Nothing as equals; consequently, a VB.NET programmer may have to check for both values.

The variation in support may cause confusion, but a developer rarely develops in both languages simultaneously. On the other hand, Microsoft provides a uniform method for working with null values: The base System.Convert namespace includes the DBNull object.

The use of the DBNull class indicates the absence of a known value. While the Microsoft documentation says it's typically in a database application, you may also use it with any type of data.

In database applications, a null object is a valid value for a field. This class differentiates between a null value (a null object) and an uninitialised value (the DBNull.Value instance). For example, a table can have records with uninitialised fields. By default, these uninitialised fields have the DBNull value.

You can utilise the DBNull object by way of its Value property. This represents a null value, which is never equal to anything. The following C# snippet revises the previous example to utilise the DBNull class:

if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine("sTest is null.");

Notice how this code utilises the string class's equality method. The VB.NET sample may utilise the same code, like this:

If (sTest.Equals(System.DBNull.Value) Then
Console.WriteLine("sTest is Null")
End  If

You may also use the DBNull.Value constant on the right-hand of an assignment operation, thus storing a null value within a reference type. In addition, the .NET Framework provides the IsDBNull function (in the System.Convert namespace), which is a shorthand approach for determining if a reference type contains a null value. The following VB.NET uses IsDBNull to check an object's value:

Dim oTest As Object = DBNull.Value
If ((IsDBNull(oTest))) Then
Console.WriteLine("oTest is Null")
End If

Even if you check for null values at every conceivable location within your code, they still may creep into the application. Fortunately, the try/catch exception block provides another line of defense; it allows you to catch any unhandled exceptions that null values cause. The System namespace provides the NullReferenceException for these situations. See how the following C# code uses NullReferenceException.

try {
if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine("sTest is null.");

} catch (System.NullReferenceException e) {
Console.WriteLine("Error: " + e.ToString());

Handle your data with care
Applications don't always receive the type of data they expect; for instance, an app may be looking for integer values and receive string or null values. Therefore, since data is the backbone of an application, you must be extremely careful when working with it.

Editorial standards