TheDeveloperBlog.com


VB.NET Nothing: Null Reference

Nothing. An object may not exist. A reference may point to nothing. We use the literal Nothing to indicate a null, nil, nonexistent object.


We assign to Nothing, test against it, and ponder nothingness. A NullReferenceException is caused by accessing something on a Nothing value.


An example. First, this program assigns a String variable to Nothing. This is not an empty string. It is a String object that does not exist. We compare it to Nothing in an If-statement.

Caution: You may get unexpected results when using "=" and Nothing. For reference types, please use "Is".

Then: We use the IsNothing function to see if a reference is equal to Nothing. This is another syntax form for the same thing.

Based on:

.NET 4.5

VB.NET program that tests against Nothing

Module Module1

    Sub Main()
	' This reference equals Nothing.
	Dim s As String = Nothing

	' We can directly test against Nothing.
	If s Is Nothing Then
	    Console.WriteLine("1")
	End If

	' We can use the IsNothing function.
	If IsNothing(s) Then
	    Console.WriteLine("2")
	End If
    End Sub

End Module

Output

1
2

NullReferenceException. This exception (not NothingReferenceException) is provoked by accessing a member on Nothing. If a reference may be Nothing, we must test before using its members.

Also: For the String type, an IsNullOrEmpty method exists. This returns true for a String that equals Nothing.

IsNullOrEmpty
VB.NET program that causes NullReferenceException

Module Module1

    Sub Main()
	' Assign a String to Nothing.
	Dim s As String = Nothing

	' This causes a NullReferenceException.
	Dim len As Integer = s.Length

	' Not reached.
	Console.WriteLine(len)
    End Sub

End Module

Output

Unhandled Exception: System.NullReferenceException:
Object reference not set to an instance of an object.

Module. When a Module or Class is created, the fields within it are automatically set to Nothing. You never need to assign them to Nothing at creation time.

Tip: Variables in VB.NET never have garbage values. In managed code, like the .NET Framework, initial values are 0 or Nothing.

VB.NET program that uses module-level field

Imports System.Text

Module Module1

    ''' <summary>
    ''' A module-level field.
    ''' </summary>
    Dim s As StringBuilder

    Sub Main()
	' The field is nothing when the program begins.
	If IsNothing(s) Then
	    Console.WriteLine(True)
	End If
    End Sub

End Module

Output

True

Values. Nothing can be used with value types such as Integers. When used on values, "Nothing" indicates the default value for the type. So an Integer assigned to Nothing is zero.

Tip: Structures are too a value type. When we assign them to Nothing, we get an empty Structure (with no bits set to 1).

VB.NET program that uses values, Nothing

Module Module1
    Sub Main()
	' Initialize an Integer to Nothing.
	Dim size As Integer = Nothing

	' It equals zero.
	Console.WriteLine(size)

	If size = 0 Then
	    Console.WriteLine("Size is 0")
	End If

	If size = Nothing Then
	    Console.WriteLine("Size is nothing")
	End If
    End Sub
End Module

Output

0
Size is 0
Size is nothing

Some research. Nothing is not just like null in C#. Instead, it has a special behavior when used with value types—it acts like the default.

Note: Thanks to Matthieu Penant for pointing out that Nothing has a separate meaning on value types.

In Visual Basic, if you set a variable of a non-nullable value type to Nothing, the variable is set to the default value for its declared type.

Nothing: MSDN

Is, IsNot. References can be compared with the "Is" and "IsNot" operators. In VB.NET, we use the Is Nothing and IsNot Nothing expressions to test class references.

Class: Is, IsNot

A summary. Nothing has two meanings. With references, it means a reference that points to no object. And with values, it means the default (zero-initialized) value.


On references, Nothing is null. It may provoke a NullReferenceException. As VB.NET developers, we must understand that "null" refers to Nothing. The terms are confusing.