dotnet/maui · error · ArgumentNullException
The parameter cannot be either null or empty.
Error message
The parameter cannot be either null or empty.
What it means
Verify.IsNeitherNullNorEmpty is an argument-validation helper that throws ArgumentNullException when the supplied string value is null. It is intended as a guard clause at the top of public methods. The paramName is taken from the 'name' argument so the exception points at the offending parameter.
Source
Thrown at src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/Standard/Verify.cs:62
/// <summary>
/// Ensure that an argument is neither null nor empty.
/// </summary>
/// <param name="value">The string to validate.</param>
/// <param name="name">The name of the parameter that will be presented if an exception is thrown.</param>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
[SuppressMessage("Microsoft.Performance", "CA1820:TestForEmptyStringsUsingStringLength")]
[DebuggerStepThrough]
public static void IsNeitherNullNorEmpty(string value, string name)
{
// catch caller errors, mixing up the parameters. Name should never be empty.
Assert.IsNeitherNullNorEmpty(name);
// Notice that ArgumentNullException and ArgumentException take the parameters in opposite order :P
const string errorMessage = "The parameter cannot be either null or empty.";
if (null == value)
{
Assert.Fail();
throw new ArgumentNullException(name, errorMessage);
}
if ("" == value)
{
Assert.Fail();
throw new ArgumentException(errorMessage, name);
}
}
/// <summary>
/// Ensure that an argument is neither null nor does it consist only of whitespace.
/// </summary>
/// <param name="value">The string to validate.</param>
/// <param name="name">The name of the parameter that will be presented if an exception is thrown.</param>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
[SuppressMessage("Microsoft.Performance", "CA1820:TestForEmptyStringsUsingStringLength")]
[DebuggerStepThrough]
public static void IsNeitherNullNorWhitespace(string value, string name)
{View on GitHub (pinned to f377ff1c5e)
Solutions
- Pass a non-null, non-empty string — initialize the value before calling the API.
- Guard at the call site with a null check and a meaningful default or early return.
- Use nameof(parameter) so the exception message names the correct parameter.
Example fix
// before DoWork(path); // path may be null // after if (path == null) throw new ArgumentNullException(nameof(path)); DoWork(path);
Defensive patterns
Strategy: validation
Validate before calling
// Pre-check for null before calling the guarded API
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
SomeApi(value); Prevention
- Initialize all string fields and parameters before forwarding them to guarded APIs.
- Use nullable reference types (string?) to surface null-safety at compile time.
- Apply nameof(parameter) so exceptions name the correct argument.
When it happens
Trigger: Calling a public method whose first lines run Verify.IsNeitherNullNorEmpty(value, nameof(value)) and passing null for value. The Assert.Fail() that precedes the throw only fires in debug builds; the exception always throws.
Common situations: Caller omitted a required string argument; a property backing field was never initialized; a config or path string came back null from a lookup and was forwarded without a null check.
Related errors
- The parameter cannot be either null or empty or consist only
- The parameter must not be the default value.
- The parameter must be null.
- The URI must be absolute.
- The integer value must be bounded with [{0}, {1})
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/5825c779b5f989c2.
Report an issue: GitHub.