Training
Module
Learn coding practices to help prevent the occurrence of NullReferenceException.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
You can disassociate an object variable from any object instance by setting it to Nothing.
Set the variable to Nothing
in an assignment statement.
' Assume account is a defined class
Dim currentAccount As account
currentAccount = Nothing
If your code tries to access a member of an object variable that has been set to Nothing
, a NullReferenceException occurs. If you set an object variable to Nothing
frequently, or if it is possible the variable is not initialized, it is a good idea to enclose member accesses in a Try...Catch...Finally
block.
If you use an object variable for objects that contain confidential or sensitive data, you can set the variable to Nothing
when you are not actively dealing with one of those objects. This reduces the chance of malicious code gaining access to the data.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Module
Learn coding practices to help prevent the occurrence of NullReferenceException.