C# ArgumentNullException — A required argument was passed as null
ArgumentNullException: value cannot be null
Verified against .NET 8 documentation, ArgumentNullException.ThrowIfNull API docs, C# coding conventions (docs.microsoft.com) · Updated June 2026
> quick_fix
Use ArgumentNullException.ThrowIfNull(param) in .NET 6+ at the top of your method, or throw new ArgumentNullException(nameof(param)) in earlier versions. Then fix the call site that is passing null.
// .NET 6+ (preferred)
public void Process(Order order)
{
ArgumentNullException.ThrowIfNull(order);
// safe to use order here
}
// Pre-.NET 6
public void Process(Order order)
{
if (order == null)
throw new ArgumentNullException(nameof(order));
}
// C# 10 null parameter check syntax (!! was removed, use ThrowIfNull)What causes this error
ArgumentNullException is thrown explicitly by code (not by the CLR) when a method receives a null argument for a parameter that requires a non-null value. It is the correct exception to throw for input validation at method entry points, as opposed to NullReferenceException which is thrown by the runtime mid-execution.
How to fix it
- 01
step 1
Read the ParamName from the exception
ArgumentNullException includes a ParamName property that names the null parameter. The message is 'Value cannot be null. (Parameter \'paramName\')'. Find every call site that invokes the method and check which argument is null.
- 02
step 2
Fix the call site, not just the method
The exception correctly identifies that null was passed. The real bug is at the call site. Find where the argument value originates — database query, deserialization, user input — and ensure it is non-null before the call, or handle the null case separately.
- 03
step 3
Add guard clauses to your own methods
Every public method that requires non-null parameters should validate them at entry: `ArgumentNullException.ThrowIfNull(param);`. This creates a clear contract and produces a better error message than a NullReferenceException buried inside the method body.
- 04
step 4
Use nullable annotations to communicate intent
With nullable reference types enabled, mark parameters that accept null as `string?` and parameters that must be non-null as `string`. The compiler will then enforce this contract at call sites, and you can remove the runtime ThrowIfNull for non-nullable parameters.
- 05
step 5
Handle optional parameters with default values
If null is a legitimate input (optional parameter), accept `string? param = null` and handle the null case explicitly inside the method rather than throwing ArgumentNullException.
How to verify the fix
- Pass null to the method — ArgumentNullException should throw immediately with the correct ParamName
- Fix the call site so null is no longer passed — the exception should not occur in normal flow
- Run all unit tests for the affected method
Why ArgumentNullException happens at the runtime level
ArgumentNullException is a deliberate guard exception, not a CLR-detected error. It exists because C# (pre-nullable-reference-types) had no way to express at the type-system level that a parameter must be non-null. The convention of throwing ArgumentNullException at method entry normalizes null-checking: rather than getting a cryptic NullReferenceException 10 stack frames deep when a null finally gets dereferenced, you get an immediate, clear exception at the exact API boundary where the contract was violated.
Common debug mistakes for ArgumentNullException
- Using the wrong string in `throw new ArgumentNullException("order")` — should always use `nameof(order)` so it survives refactoring
- Throwing ArgumentNullException in the middle of a method instead of at the top — let invalid input fail fast
- Not using ThrowIfNull in .NET 6+ codebases — the static helper is safer and more readable than a manual if-null-throw block
- Catching ArgumentNullException in the calling code and silently swallowing it — this hides bugs instead of fixing them
When ArgumentNullException signals a deeper problem
ArgumentNullException is a code-smell radar: when it appears frequently in logs, it means callers do not know (or do not check) whether a given method accepts null. The structural fix is to encode nullability in types with C# nullable reference types, so the compiler enforces the contract before the code runs. ArgumentNullException then becomes a runtime safety net for external input rather than a substitute for a type-safe API.
Editor's take
ArgumentNullException is the C# ecosystem's way of telling you that a method received a null where it expected a real value — and the method author decided to fail fast rather than let the null propagate silently. In production .NET services, this exception is one of the top three causes of unhandled crashes in ASP.NET Core request pipelines, particularly when dependency injection resolves a service to null because a registration was missed in Program.cs. The worst variant is when it surfaces inside a third-party library you don't control: the stack trace points to internal code, and the actual bug is three calls up where your code passed null without realizing. Senior C# developers learn to treat ArgumentNullException as a boundary guard — you throw it at the top of public methods to document your contract explicitly. If you're seeing this in your own code, the fix is almost always adding a null check or using the C# 10+ `ArgumentNullException.ThrowIfNull()` one-liner. If you're catching it from framework code, trace the parameter name in the exception message back to your caller. The parameter name is always included — read it. Engineers who understand this pattern write more defensive APIs and spend less time debugging null-propagation chains that crash three layers deep.
By Bikram Nath · Curator · Updated June 2026
Frequently asked questions
Should I throw ArgumentNullException or NullReferenceException?
Always throw ArgumentNullException when validating method parameters. NullReferenceException is thrown by the CLR when you accidentally dereference null — it is not something you should throw manually. ArgumentNullException communicates intent: 'the caller passed null and should not have.'
What is the difference between ArgumentNullException and ArgumentException?
ArgumentException is the base class for all argument validation exceptions. ArgumentNullException is a subclass specifically for null arguments. ArgumentOutOfRangeException is another subclass for arguments outside an acceptable range. Use the most specific subclass to communicate the exact violation.
Does .NET dependency injection (DI) throw ArgumentNullException?
Yes — if a service registered with the DI container cannot be resolved (e.g., it was not registered), injecting it into a constructor will typically cause either an InvalidOperationException at activation time, or an ArgumentNullException if the injected dependency is used without a null check.