What is a NullReferenceException, and how do I fix it?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A NullReferenceException in C# means your code tried to use an object reference that was null. In plain terms, you asked for a member such as a property, field, method, or index on something that does not point to a real object.
The fix is rarely "catch the exception and continue." The real fix is to find out why the value was null in the first place and then make that state impossible, expected, or safely handled.
What Actually Throws It
These all throw NullReferenceException:
In each case, the variable itself is not the problem. The problem is that the variable contains no object reference at runtime.
The First Job: Find Which Value Is Null
When you see a long expression such as order.Customer.Address.City, the exception means one link in that chain was null, not necessarily the first variable.
Break it apart:
Or inspect it in the debugger. That tells you whether order, Customer, or Address is the missing value.
This is the most important debugging step because it turns a vague crash into a concrete cause.
Fix by Initializing Required Objects
If a value is required for the object to be valid, initialize it before use:
Now this no longer throws:
This is often the best fix for domain objects that should never be partially initialized.
Fix by Guarding Optional Values
Sometimes null is valid and means "missing" or "not found." In that case, guard it explicitly:
Or use the null-conditional and null-coalescing operators when a fallback makes sense:
That does not make null disappear. It just handles it intentionally.
Throw the Right Exception at Boundaries
If a method requires a non-null argument, fail early with ArgumentNullException instead of letting a later NullReferenceException happen deeper inside:
This produces a clearer error message and points directly at the broken contract.
Nullable Reference Types Help Prevent It
Modern C# can warn you about potential null misuse before runtime when nullable reference types are enabled:
The compiler warning is not the same as runtime safety, but it catches many problems much earlier.
Do Not Treat Catching It as the Main Fix
This is usually the wrong instinct:
That hides the real bug. Exceptions are for exceptional situations, not for normal null-driven control flow. Prefer validation, initialization, or explicit optional handling.
Common Pitfalls
The most common pitfall is debugging the line but not the source. The crash may occur at person.Address.City, but the real mistake might be that a repository returned null earlier and nobody handled it.
Another is assuming object creation initializes nested objects automatically. new Person() does not magically create Address unless your class does so.
Teams also ignore nullable warnings from the compiler. Those warnings exist because the runtime exception is expensive and avoidable.
Finally, catching NullReferenceException broadly is almost always a poor substitute for fixing the data flow.
Summary
- '
NullReferenceExceptionmeans your code dereferenced anullobject reference.' - Find the exact missing value before choosing a fix.
- Initialize required objects so invalid states are harder to create.
- Guard optional values explicitly with checks or null-aware operators.
- Use nullable reference types and
ArgumentNullExceptionto catch problems earlier and more clearly.

