Export (0) Print
Expand All
Expand Minimize

Troubleshooting Exceptions: System.NullReferenceException

A NullReferenceException occurs when you try to reference an object in your code that does not exist. For example, you may have tried to use an object without using the New keyword first, or tried to use an object whose value is set to null (Nothing in Visual Basic).

Use the New keyword to create the instance.

You may have tried to use an object without providing an instance of the object. For example, Dim CustomerTable As DataTable should be rewritten as Dim CustomerTable As New DataTable.

Include a code block that checks for null references.

Programmatically check to determine if a function has returned null (Nothing in Visual Basic) instead of an instance of an object.

Explicitly catch NullReferenceException in a Try…Catch…Finally statement.

A Try…Catch…Finally statement can check for specific types of exceptions, going from most specific to least specific.

Community Additions

© 2015 Microsoft