Export (0) Print
Expand All
7 out of 33 rated this helpful - Rate this topic

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.