dotnet/wpf · error · ArgumentException

The parameter must be null.

Error message

The parameter must be null.

What it means

Verify.IsNull<T> is the inverse of a null check: it asserts that a reference-type argument IS null, throwing ArgumentException if it is not. It is used to enforce that some object has already been released/reset (e.g. a disposed or detached resource must be null).

Solutions

  1. Set the field/variable to null (or Dispose and null it) before calling Verify.IsNull.
  2. If the object legitimately can be non-null, remove this assertion or replace it with a state check that matches the real contract.
  3. Ensure disposal paths actually run (check for early returns/exceptions skipping cleanup).
  4. Use ex.ParamName to identify which reference was expected to be null.

Example fix

// before
Verify.IsNull(_sink, nameof(_sink)); // _sink still holds old reference
// after
_sink?.Dispose();
_sink = null;
Verify.IsNull(_sink, nameof(_sink));
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj != null)
    throw new InvalidOperationException($"{nameof(obj)} must already be null (released)");
Verify.IsNull(obj, nameof(obj));

Type guard

static bool IsReleased<T>(T obj) where T : class => obj is null;

Try / catch

try {
    Verify.IsNull(_resource, nameof(_resource));
} catch (ArgumentException ex) {
    (_resource as IDisposable)?.Dispose();
    _resource = null;
}

Prevention

When it happens

Trigger: Calling Verify.IsNull(obj, name) when obj is a live, non-null reference — e.g. asserting a handle/dispatcher/resource was cleared but it was never set to null.

Common situations: Teardown/dispose code asserting state reset; refactors that changed cleanup logic so a field is no longer nulled; object reuse without clearing prior references.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

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

        [DebuggerStepThrough]
        public static void IsNotNull<T>(T obj, string name) where T : class
        {
            if (null == obj)
            {
                throw new ArgumentNullException(name);
            }
        }

        /// <summary>Verifies that an argument is 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 IsNull<T>(T obj, string name) where T : class
        {
            if (null != obj)
            {
                throw new ArgumentException("The parameter must be null.", name);
            }
        }
        
        [DebuggerStepThrough]
        public static void PropertyIsNotNull<T>(T obj, string name) where T : class
        {
            if (null == obj)
            {
                throw new InvalidOperationException($"The property {name} cannot be null at this time.");
            }
        }
        
        [DebuggerStepThrough]
        public static void PropertyIsNull<T>(T obj, string name) where T : class
        {
            if (null != obj)
            {
                throw new InvalidOperationException($"The property {name} must be null at this time.");

View on GitHub (pinned to 81131a70a4)