dotnet/wpf · error · System.ArgumentNullException

SR.Verify_NeitherNullNorEmpty

Error message

SR.Verify_NeitherNullNorEmpty

What it means

Verify.IsNeitherNullNorEmpty rejects a string argument that is null (ArgumentNullException) or the empty string "" (ArgumentException), with the parameter's name attached. The library treats an empty string as indistinguishable from a missing value, so callers must pass a real, non-empty value. The doc comment notes the parameter 'name' should never itself be empty - that would indicate a caller bug mixing up parameters.

Solutions

  1. Inspect the call stack to identify which named parameter was empty; fix the value source so a real non-empty string is supplied.
  2. Guard before calling: if (string.IsNullOrEmpty(value)) throw/log with context instead of letting the library throw.
  3. Fix configuration/user input defaults so required strings are never empty (use meaningful defaults or fail fast at startup).
  4. If empty is legitimately allowed, don't route through this verify method - check before calling.

Example fix

// before
Verify.IsNeitherNullNorEmpty(config.OutputPath, "OutputPath"); // config.OutputPath == ""
// after
if (string.IsNullOrEmpty(config.OutputPath))
    config.OutputPath = DefaultOutputPath; // or fail fast with a clear message
Verify.IsNeitherNullNorEmpty(config.OutputPath, "OutputPath");
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(value))
    throw new ArgumentException($"{nameof(value)} must be a non-empty string.", nameof(value));

Type guard

bool IsNonEmpty(string? s) => !string.IsNullOrEmpty(s);

Try / catch

try
{
    LibraryCall(value);
}
catch (ArgumentNullException ex) when (ex.ParamName == "value")
{
    logger.LogWarning("Required string '{Param}' was null", ex.ParamName);
}
catch (ArgumentException ex) when (ex.ParamName == "value")
{
    logger.LogWarning("Required string '{Param}' was empty", ex.ParamName);
}

Prevention

When it happens

Trigger: Passing null or "" as the 'value' argument to Verify.IsNeitherNullNorEmpty(value, name) - directly or via wrappers such as Verify.FileExists where the path argument is empty.

Common situations: Path/name strings built from configuration or user input that end up empty (missing config key, blank text box, unexpanded environment variable); string fields default-initialized to "" instead of null and then validated.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/Verify.cs:48

            {
                throw new InvalidOperationException(SR.Format(SR.Verify_ApartmentState, requiredState));
            }
        }

        /// <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>
        public static void IsNeitherNullNorEmpty(string value, string name)
        {
            // catch caller errors, mixing up the parameters.  Name should never be empty.
            Debug.Assert(!string.IsNullOrEmpty(name));

            // Notice that ArgumentNullException and ArgumentException take the parameters in opposite order :P
            if (value == null)
            {
                throw new ArgumentNullException(name, SR.Verify_NeitherNullNorEmpty);
            }
            if (value == "")
            {
                throw new ArgumentException(SR.Verify_NeitherNullNorEmpty, 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>
        public static void IsNotNull<T>(T obj, string name) where T : class
        {
            if (obj == null)
            {
                throw new ArgumentNullException(name);
            }
        }

View on GitHub (pinned to 81131a70a4)