C# NullReferenceException — Object reference not set to an instance of an object
NullReferenceException: object reference is null
Verified against .NET 8 documentation, C# language specification §8.9.5, Nullable reference types guide (docs.microsoft.com) · Updated June 2026
> quick_fix
Use the null-conditional operator (?.) to safely access members, or add a null check before accessing the object. In .NET 6+, enable nullable reference types to get compile-time warnings.
// Option 1: null-conditional operator
string? name = user?.Profile?.Name;
// Option 2: null-coalescing
string name = user?.Name ?? "Anonymous";
// Option 3: explicit null check
if (user != null)
{
Console.WriteLine(user.Name);
}
// Option 4: C# 8 pattern matching
if (user is { Name: var name })
{
Console.WriteLine(name);
}What causes this error
NullReferenceException is thrown when you attempt to access a member (property, method, indexer) on a reference-type variable whose value is null. This is the C# equivalent of Java's NullPointerException. Common sources include uninitialized fields, LINQ queries returning null, EF Core navigation properties not loaded, and method return values not checked.
How to fix it
- 01
step 1
Identify the null variable from the stack trace
The exception message in .NET 6+ says 'Object reference not set to an instance of an object' and the stack trace points to the exact line. In Visual Studio, hover over each chained member on that line in the debugger to find which one is null.
- 02
step 2
Use the null-conditional operator (?.) for chains
Replace `obj.Prop.SubProp` with `obj?.Prop?.SubProp`. The expression short-circuits to null if any part is null, preventing the exception. Combine with ?? to supply a default: `obj?.Prop?.SubProp ?? "default"`.
- 03
step 3
Enable nullable reference types (C# 8 / .NET 6+)
Add `<Nullable>enable</Nullable>` to your .csproj. The compiler then distinguishes `string` (non-nullable) from `string?` (nullable) and warns you at compile time wherever you dereference a nullable without a null check.
- 04
step 4
Check EF Core navigation property loading
If the null is a navigation property (e.g., order.Customer is null), you likely forgot to include it: `dbContext.Orders.Include(o => o.Customer).FirstOrDefault(...)`. Lazy loading requires virtual properties and the proxies package.
- 05
step 5
Validate constructor and factory inputs
Use ArgumentNullException.ThrowIfNull(param) (C# 10+) or throw new ArgumentNullException(nameof(param)) at the top of constructors and public methods to fail fast at the call site rather than deep inside the object graph.
How to verify the fix
- Re-run the failing code path — NullReferenceException must not recur
- Check that nullable-enabled code compiles with zero CS8600/CS8602 warnings
- Unit-test the null path: pass null inputs and confirm the fallback value or exception message is as expected
Why NullReferenceException happens at the runtime level
In C#, all reference types (classes, arrays, delegates, interfaces) can hold the value null, which represents the absence of an object reference. When the CLR executes an instruction that dereferences a reference-type variable — reading a property, calling a method, accessing an indexer — it first checks whether the reference is null. If it is, the CLR throws System.NullReferenceException, a subclass of SystemException. Prior to C# 8, the type system provided no compile-time distinction between nullable and non-nullable references, making null the most common runtime error in C# codebases.
Common debug mistakes for NullReferenceException
- Calling FirstOrDefault() on a LINQ query and immediately accessing a property without checking for null
- Not including EF Core navigation properties with .Include() and then accessing them
- Deserializing JSON with System.Text.Json into a non-nullable property that is absent from the payload
- Using string.IsNullOrEmpty() but then calling methods on the same string outside the guard block
When NullReferenceException signals a deeper problem
NullReferenceException is the .NET equivalent of the 'billion-dollar mistake' — C# historically treated every reference as potentially null without requiring the caller to acknowledge that. The nullable reference types feature in C# 8 is the long-awaited fix, but it is opt-in and requires discipline to enforce across a large codebase. Teams migrating legacy code often enable it incrementally per namespace; doing so exposes a backlog of implicit null flows that were always present but invisible.
Editor's take
NullReferenceException is the single most common runtime exception in the entire .NET ecosystem — Microsoft's own telemetry confirms it accounts for more crash reports than any other exception type. It means you tried to access a member (property, method, or field) on a reference that points to null. C# 8.0's nullable reference types were designed specifically to eliminate this class of bug at compile time, and enabling `<Nullable>enable</Nullable>` in your .csproj is the single highest-impact change you can make to reduce NREs in any codebase. In production ASP.NET services, the most dangerous NREs are the ones that happen inside middleware or filters — they short-circuit the entire request pipeline and often return a generic 500 with no useful error context. The pattern that causes the most debugging pain is chained member access: `order.Customer.Address.City` where any link in the chain could be null. The null-conditional operator `?.` and null-coalescing operator `??` exist precisely for this — use them. In Entity Framework, NREs frequently signal a missing `.Include()` for a navigation property that lazy loading would have handled in EF6 but EF Core does not load by default. If you're debugging an NRE, the stack trace tells you the exact line — the challenge is figuring out which reference on that line is null when there are multiple candidates.
By Bikram Nath · Curator · Updated June 2026
Frequently asked questions
What does 'Object reference not set to an instance of an object' mean?
It means a reference-type variable (class, array, delegate, interface) has not been assigned an object and its value is null. You then tried to call a method or read a property on it, which requires a real object in memory.
Does enabling nullable reference types prevent NullReferenceException at runtime?
Not entirely — it prevents it at compile time for code you write. Code that calls external libraries, uses reflection, deserializes JSON, or accesses database results can still produce null at runtime. The nullable annotations narrow the surface area significantly but do not eliminate the possibility.
What is the difference between NullReferenceException and ArgumentNullException?
NullReferenceException is thrown by the runtime when you dereference null. ArgumentNullException is thrown explicitly by code (typically a method) when a caller passes null where a non-null argument is required. Prefer throwing ArgumentNullException early in your own methods rather than letting NullReferenceException surface deep in your logic.