Share via


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 DataTableshould 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.

See Also

Tasks

How to: Test Code with a Try…Catch Block in Visual Basic

Reference

New (Visual Basic)
Try...Catch...Finally Statement (Visual Basic)
Visual J# Exception Handling for the Debugger
Visual J# Exception Hierarchies
NullReferenceException