ArgumentNullException
C# Programming Language
Severity: ModerateWhat Does This Error Mean?
ArgumentNullException means you passed null into a method that requires an actual value. The error message usually names the exact parameter that was null — for example: 'Value cannot be null. (Parameter: fileName)'. This is a deliberate guard — the method is telling you it cannot do its job with nothing. Fix it by making sure you provide a real value before calling the method.
Affected Models
- .NET Framework
- .NET Core
- .NET 5+
- Unity (C# scripting)
- ASP.NET
Common Causes
- Passing a variable that was never assigned a value to a method
- A method returned null and you passed that return value directly to another method
- A required configuration value, file path, or setting was not provided
- An object was supposed to be created before being passed, but something prevented the creation
- Forgetting to check for null after reading from a dictionary, database, or external source
How to Fix It
-
Read the error message carefully. It names the parameter that was null — for example 'Parameter: source' or 'Parameter: path'. This tells you exactly what is missing.
The parameter name in the error message matches a parameter name in the method you called. Find that method to understand what it expected.
-
Trace back to where that value was supposed to come from. Was it read from a file? A database? User input? Check why it ended up as null.
Set a breakpoint just before the crashing line and inspect the variable. If it is null, trace back one step further.
-
Add a null check before calling the method. If the value can legitimately be null, handle that case before proceeding.
Example: if (myValue == null) { handle the missing value or return early } else { call the method }
-
If the null comes from a method call, check whether that method can return null and handle it. Use a null-coalescing operator to provide a fallback.
Example: string path = GetPath() ?? string.Empty; — this prevents null from being passed along.
-
In .NET 6 and later, enable nullable reference types in your project. This makes the compiler warn you at build time when null might be passed where it should not be.
Add 'nullable = enable' to your .csproj file or set it in the project properties. This is the best long-term prevention.
When to Call a Professional
ArgumentNullException is a code bug you can fix yourself. The error message tells you exactly which parameter is null — start there. Trace backwards to find where that value was supposed to come from and why it is missing. If the null comes from a database or external service, add proper null checks and error handling.
Frequently Asked Questions
What is the difference between ArgumentNullException and NullReferenceException?
ArgumentNullException is thrown on purpose by a method to tell you: 'You passed me null and I cannot work with that.' NullReferenceException is thrown by the .NET runtime when your code accidentally tries to use a null object. ArgumentNullException is a helpful guard — NullReferenceException is an unguarded crash. ArgumentNullException is usually easier to fix because the message tells you exactly which argument was null.
Should I throw ArgumentNullException in my own methods?
Yes — this is a good practice. If your method requires a non-null argument to work, check for null at the top and throw ArgumentNullException if it is missing. In .NET 6+, use ArgumentNullException.ThrowIfNull(myParam) — it is one line and very readable. This protects your method and gives callers a clear, helpful error message.
Can I use a default value instead of throwing an exception?
Yes — it depends on your situation. If null is a valid but unusual case, handle it gracefully rather than crashing. For example: use an empty string instead of null, or return early with a default result. Only throw the exception if null truly means 'this cannot proceed' — which is often the case.