dotnet/wpf · error · ArgumentNullException

The parameter can not be either null or empty or consist…

Error message

The parameter can not be either null or empty or consist only of white space characters.

What it means

Verify.IsNeitherNullNorWhitespace validates that a string argument is not null, not empty, and not composed only of whitespace. A null value throws ArgumentNullException (Verify.cs:86) with the shared message about whitespace. Like the other Verify helpers it is a fail-fast precondition used across WPF's Standard utility layer.

Solutions

  1. Guard with string.IsNullOrWhiteSpace(value) before the call and supply a valid string.
  2. Provide a default/fallback value when the input is optional.
  3. Fix the upstream producer so it does not emit null for this field.
  4. Inspect ex.ParamName to identify which argument was null.

Example fix

// before
Verify.IsNeitherNullNorWhitespace(configValue, nameof(configValue)); // may be null
// after
if (string.IsNullOrWhiteSpace(configValue)) configValue = DefaultConfigValue;
Verify.IsNeitherNullNorWhitespace(configValue, nameof(configValue));
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(value))
    throw new ArgumentException($"{nameof(value)} must be non-null, non-empty, non-whitespace", nameof(value));
Verify.IsNeitherNullNorWhitespace(value, nameof(value));

Type guard

static bool IsNonWhitespaceString(string s) => !string.IsNullOrWhiteSpace(s);

Try / catch

try {
    Verify.IsNeitherNullNorWhitespace(value, nameof(value));
} catch (ArgumentNullException ex) {
    log.Error($"{ex.ParamName} was null");
}

Prevention

When it happens

Trigger: Calling Verify.IsNeitherNullNorWhitespace(value, name) with value == null — throws ArgumentNullException carrying the message 'The parameter can not be either null or empty or consist only of white space characters.'

Common situations: Config values or user input that were never set (null) being passed where a real string is required; interop paths passing unset names/keys.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/0dc9273342fec751. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Standard/Verify.cs:86

        }

        /// <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", "CA1820:TestForEmptyStringsUsingStringLength")]
        [DebuggerStepThrough]
        public static void IsNeitherNullNorWhitespace(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 can not be either null or empty or consist only of white space characters.";
            if (null == value)
            {
                throw new ArgumentNullException(name, errorMessage);
            }
            if ("" == value.Trim())
            {
                throw new ArgumentException(errorMessage, name);
            }
        }

        /// <summary>Verifies that an argument is not null.</summary>
        /// <typeparam name="T">Type of the object to validate.  Must be a class.</typeparam>
        /// <param name="obj">The object to validate.</param>
        /// <param name="name">The name of the parameter that will be presented if an exception is thrown.</param>        
        [DebuggerStepThrough]
        public static void IsNotDefault<T>(T obj, string name) where T : struct
        {
            if (default(T).Equals(obj))
            {
                throw new ArgumentException("The parameter must not be the default value.", name);
            }

View on GitHub (pinned to 81131a70a4)