NullReferenceException .NET Origin: Application & API Last updated 14 Oct 2026

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.

Exact message you may see Object reference not set to an instance of an object
Seen on: .NET

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

  1. Find the line in the stack trace (enable PDBs in release)
  2. Use ?. and ??
  3. Check FirstOrDefault() results
  4. Enable nullable reference types

Detailed fix by platform

.NET

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

  1. Line — Which expression?
  2. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

An unhandled error has occurred — Blazor: An unhandled error has occurred. Reload 🗙 (.NET)An unhandled error ha…AzureWebJobsStorage — Azure Functions: Value cannot be null. (Parameter 'connectionString') / Missing value for AzureWebJobsStorage (Azure)AzureWebJobsStorageCall to member function on null — Fatal error: Uncaught Error: Call to a member function x() on null (PHP)Call to member functi…CS8618 — warning CS8618: Non-nullable property 'X' must contain a non-null value when exiting constructor. Consider declaring the property as nullable (.NET)CS8618InvalidOperationException — InvalidOperationException: Unable to resolve service for type 'X' while attempting to activate 'Y' (.NET)InvalidOperationExcep…NullPointerException — java.lang.NullPointerException (Java)NullPointerExceptionSequence contains no elements — InvalidOperationException: Sequence contains no elements / more than one element (.NET)Sequence contains no …TypeError — TypeError: Cannot read properties of undefined (reading 'x') (JavaScript)TypeErrorNullRefer…

Explore the map of this error Full error map →

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