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).
Associated Tips
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.For more information, Visual Basic users can see Nothing (Visual Basic).
For more information, C# users can see null (C# Reference).
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.For more information, Visual Basic users can see Try...Catch...Finally Statement (Visual Basic).
For more information, C# users can see try-catch-finally (C# Reference).
See Also
Tasks
How to: Test Code with a Try…Catch Block in Visual Basic