System.NullReferenceException: Object reference not set to an instance of an object
Code dereferenced a variable that is null — the .NET equivalent of Java’s NullPointerException.
Object reference not set to an instance of an object Meaning
The stack trace line shows where; enable nullable reference types (<Nullable>enable</Nullable>) so the compiler warns about possible nulls.
Common causes
- Uninitialized object or collection
- Method/LINQ returned null (
FirstOrDefault) - Dependency not registered in DI
- Model binding produced null
⚡ Quick fix
- Find the line in the stack trace (enable PDBs in release)
- Use
?.and?? - Check
FirstOrDefault()results - Enable nullable reference types
Detailed fix by platform
.NET
- Null-safe access:csharp
var city = user?.Address?.City ?? "Unknown"; var order = orders.FirstOrDefault(o => o.Id == id) ?? throw new KeyNotFoundException($"Order {id}");
How to diagnose
- Line — Which expression?
- Source — Where should the value come from?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- An unhandled error has occurred Blazor: An unhandled error has occurred. Reload 🗙
- AzureWebJobsStorage Azure Functions: Value cannot be null. (Parameter 'connectionString') / Missing value for AzureWebJobsStorage
- Call to member function on null Fatal error: Uncaught Error: Call to a member function x() on null
Most viewed in .NET
- FileNotFoundException assembly Could not load file or assembly 'X, Version=…' or one of its dependencies
- Unable to cast object of type System.InvalidCastException: Unable to cast object of type 'X' to type 'Y'
- Cannot access a disposed context instance ObjectDisposedException: Cannot access a disposed context instance
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 14 Oct 2026