NullReferenceException
C# Programming Language
Severity: ModerateWhat Does This Error Mean?
NullReferenceException means your code tried to use an object that has no value — it is null. In plain English: you tried to open a door that does not exist. This is one of the most common errors in C#. It always means something was never assigned a value before you tried to use it.
Affected Models
- .NET Framework
- .NET Core
- .NET 5+
- Unity (C# scripting)
- ASP.NET
Common Causes
- A variable was declared but never assigned an object before being used
- A method returned null and the return value was used directly without a null check
- An object in a list or array is null at the index you accessed
- A class field or property was never initialized in the constructor
- An optional dependency (like a service or component) was not injected or set up correctly
How to Fix It
-
Read the error message carefully. It will tell you the file name and line number where the crash happened. Go to that exact line.
The line number is your starting point. Open the file and look at what objects are being used on that line.
-
Look at every object used on that line. Ask yourself: could any of these be null at this point in the code? Add a null check before using it.
Example: instead of 'myObject.Name', write 'if (myObject != null) { use myObject.Name }'
-
Trace backwards in the code to find where that object was supposed to be assigned. Check if the assignment was skipped, returned null, or never reached.
Common culprits: a method that returns null on failure, a list element that was never set, or a constructor that skips initialization.
-
Use the null-conditional operator (?.) to safely access members. This returns null instead of throwing an exception.
Example: 'string name = myObject?.Name;' — if myObject is null, name becomes null instead of crashing.
-
Consider using the null-coalescing operator (??) to provide a default value when something is null.
Example: 'string name = myObject?.Name ?? "Unknown";' — if null, name becomes 'Unknown' instead of crashing.
When to Call a Professional
NullReferenceException is a code bug — it does not require outside help. If you cannot find the source, use a debugger to step through the code line by line. In Visual Studio, set a breakpoint near the crash and hover over variables to see which one is null. If this is in a large inherited codebase, a senior developer can help trace the root cause.
Frequently Asked Questions
How do I find which variable is null?
Run your program in debug mode (F5 in Visual Studio). When the exception appears, click 'Break' to pause at the crash. Hover your mouse over each variable on the crashing line. The one showing 'null' is your problem. You can also use the Locals or Watch window to inspect all variables.
Can NullReferenceException be prevented completely?
Yes — with good habits. Always initialize objects before using them. Use nullable reference types (enabled in .NET 6+ projects) which warn you at compile time. Use null checks or the ?. operator whenever something might be null. Unit tests that cover edge cases also catch these early.
Why does my code sometimes crash and sometimes not?
This usually means the null situation only happens under certain conditions. For example: the crash only happens when a list is empty, or when a user skips a field, or when an API call fails. Add null checks around the suspicious code and log when null is encountered to understand the pattern.