dotnet/wpf · error · ArgumentNullException

ArgumentNullException(name)

Error message

ArgumentNullException(name)

What it means

Verify.IsNotNull<T>(T obj, string name) throws ArgumentNullException(name) when the argument is a null reference. It is a lightweight null-check helper for reference types (constraint 'where T : class') used across WindowsBase to fail fast with the offending parameter name. The exception carries no custom message - only the parameter name.

Solutions

  1. Read the ParamName on the ArgumentNullException from the stack trace to find which argument was null and stop passing null.
  2. Add your own null check with a descriptive message before calling the API.
  3. Initialize the object properly (constructor, XAML resource, factory) rather than defaulting to null.
  4. If null is a valid state on your side, guard the call site with an if (obj != null) check.

Example fix

// before
target.DoSomething(null); // ArgumentNullException: name
// after
if (value == null) throw new ArgumentNullException(nameof(value), "value is required");
target.DoSomething(value);
Defensive patterns

Strategy: validation

Validate before calling

if (obj is null)
    throw new ArgumentNullException(nameof(obj), "obj must not be null before calling this API.");

Type guard

bool NotNull<T>([NotNullWhen(true)] T? obj) where T : class => obj is not null;

Try / catch

try
{
    LibraryCall(obj);
}
catch (ArgumentNullException ex) when (ex.ParamName == "obj")
{
    logger.LogError("Required argument {Param} was null", ex.ParamName);
    throw;
}

Prevention

When it happens

Trigger: Passing null as 'obj' to any internal API that calls Verify.IsNotNull (e.g. null event args, null dependency object, null resource).

Common situations: WPF event handlers or callbacks receiving null references from misconfigured XAML or templates; factory methods returning null; optional parameters passed as null where the library doesn't permit it.

Related errors


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

Appendix: source

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

            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);
            }
        }

        /// <summary>
        /// Verifies the specified expression is true.  Throws an ArgumentException if it's not.
        /// </summary>
        /// <param name="expression">The expression to be verified as true.</param>
        /// <param name="name">Name of the parameter to include in the ArgumentException.</param>
        /// <param name="message">The message to include in the ArgumentException.</param>
        public static void IsTrue(bool expression, string name, string message)
        {
            if (!expression)
            {
                throw new ArgumentException(message, name);
            }
        }

        /// <summary>

View on GitHub (pinned to 81131a70a4)