Diagnosing NullReferenceException in Visual Basic .NET: Nothing Checks and Initialization Order
A practical diagnostic guide for Object reference not set to an instance of an object in VB.NET. Covers cause table, ordered debugger checks, initialization and timing fixes, and verification steps.
09 Aug 2026, 18:02 UTC

Recognizable condition
The runtime error Object reference not set to an instance of an object stops execution at a line that accesses a member of a variable. In Visual Basic .NET that variable holds Nothing, the VB equivalent of null for reference types. The exception points to the member access, not to where the variable became Nothing, so the fix is tracing the variable backwards to its origin.
Useful takeaway: confirm the variable is Nothing at the exact throw site before changing logic. That prevents masking the real initialization or timing problem.
Cause to symptom table
| Typical cause | Where it appears | Diagnostic clue |
|---|---|---|
| Missing New | Class instance declared but never constructed | Variable is Nothing on first use |
| Form control accessed too early | Constructor or Load event ordering | Control is Nothing before InitializeComponent or Form_Load completes |
| Database field null mapped to non-nullable | DataReader or ORM materialization | Field read returns Nothing and is assigned to a value type or used without check |
| Null argument passed in | Method entry point | Parameter is Nothing and method dereferences it without guard |
Ordered checks
- Open the exception in Visual Studio. Run the project locally with debugging enabled. No special permissions required.
- At the exception line, hover over the variable that is being accessed. Confirm the tooltip shows Nothing. If the debugger cannot evaluate, add a Watch for the variable.
- Set a conditional breakpoint on the line before the access with condition
obj Is Nothing. This stops only when the problem state occurs and avoids stopping on healthy paths. - Trace the variable backwards. Check declaration site for
Dim x As TypewithoutNew. Check assignments from method returns, DataReader.GetValue, or control lookups. - For UI code, verify event timing. Controls created by the designer are only valid after InitializeComponent runs. Accessing them in the form constructor before that call will be Nothing.
- For data code, inspect the source for nullable columns. A database NULL becomes Nothing in VB when read into an Object or a nullable reference type.
Fixes tied to findings
Missing initialization
Declare and construct in one statement or assign before use.
Dim customer As New Customer()Risk: constructing too early can trigger side effects. Construct at the point of first logical need, not at module scope unless required.
UI timing
Move control access to Form_Load or later. Do not read controls in the constructor before InitializeComponent.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' controls are now created
Dim txt As TextBox = Me.txtName
End SubLimitation: designer generated code may rename controls. Verify the name matches the designer file.
Nullable data
Guard reads from data sources.
If Not IsDBNull(reader.GetValue(0)) Then
value = CStr(reader.GetValue(0))
End IfFor reference types, use If Not obj Is Nothing Then before member access. Avoid blanket Try Catch around the access, which hides logic errors and degrades performance.
Method arguments
Add an entry guard and fail fast with a clear message.
If arg Is Nothing Then
Throw New ArgumentNullException(NameOf(arg))
End IfThis makes the call site responsible for providing a valid instance.
Verify the fix
Re-run with the same input that triggered the exception. The conditional breakpoint should no longer hit. Hover over the variable at the former throw site to confirm it is no longer Nothing.
Implement the If Not object Is Nothing Then pattern temporarily in a test build to prove the crash is prevented. Remove the temporary guard once the root cause is fixed to keep code clean.
Limitations and escalation
Nothing checks do not fix design issues where an object should never be null. Repeated nulls from external services indicate a contract problem.
Escalate when the variable is set in a different assembly without source access, when the null originates from a third-party library, or when the exception is intermittent and depends on async timing. In those cases collect the call stack, the variable watch values, and the reproduction steps before escalating.