In VB.NET 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.
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.
IsNothing
function to see if a reference is equal to Nothing. This is another syntax form for the same thing.Module Module1 Sub Main() ' This reference equals Nothing. Dim s As String = Nothing ' Part 1: directly test against Nothing. If s Is Nothing Then Console.WriteLine("1") End If ' Part 2: use the IsNothing function. If IsNothing(s) Then Console.WriteLine("2") End If End Sub End Module1 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.
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 ModuleUnhandled 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.
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 ModuleTrue
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.
Structure
(with no bits set to 1).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 Module0 Size is 0 Size is nothing
String.IsNullOrEmpty
A string
in VB.NET can be assigned to Nothing, which is the same concept as Null. We can test a Nothing String
with the String.IsNullOrEmpty
method.
null
" is simply a different name for "Nothing." There is no "IsNothingOrEmpty
" string
method.Module Module1 Sub Main() Dim value As String = Nothing ' Nothing is the same as "Null." If String.IsNullOrEmpty(value) Then Console.WriteLine("NULL OR EMPTY") End If End Sub End ModuleNULL OR EMPTY
Nothing is not just like null
in C#. Instead, it has a special behavior when used with value types—it acts like the default.
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.
Value types cannot be equal to null
. But with the Nullable structure, we can wrap an Integer and set it to null
(with Nothing). This can help programs be clearer.
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.
null
It may provoke a NullReferenceException
. As VB.NET developers, we must understand that "null
" refers to Nothing. The terms are confusing.