AvaloniaUI/Avalonia · error · ArgumentNullException

Value cannot be null. (Parameter 'disposables')

Error message

Value cannot be null. (Parameter 'disposables')

What it means

CompositeDisposable(params IDisposable[] disposables) throws ArgumentNullException(nameof(disposables)) when the array is null, before copying it. Because it is a params array, a literal null (not an array of nulls) is the trigger; this guards against passing null as the single argument.

Source

Thrown at src/Avalonia.Base/Reactive/CompositeDisposable.cs:41

        if (capacity < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(capacity));
        }

        _disposables = new List<IDisposable?>(capacity);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="CompositeDisposable"/> class from a group of disposables.
    /// </summary>
    /// <param name="disposables">Disposables that will be disposed together.</param>
    /// <exception cref="ArgumentNullException"><paramref name="disposables"/> is <c>null</c>.</exception>
    /// <exception cref="ArgumentException">Any of the disposables in the <paramref name="disposables"/> collection is <c>null</c>.</exception>
    public CompositeDisposable(params IDisposable[] disposables)
    {
        if (disposables == null)
        {
            throw new ArgumentNullException(nameof(disposables));
        }

        _disposables = ToList(disposables);

        // _count can be read by other threads and thus should be properly visible
        // also releases the _disposables contents so it becomes thread-safe
        Volatile.Write(ref _count, _disposables.Count);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="CompositeDisposable"/> class from a group of disposables.
    /// </summary>
    /// <param name="disposables">Disposables that will be disposed together.</param>
    /// <exception cref="ArgumentNullException"><paramref name="disposables"/> is <c>null</c>.</exception>
    /// <exception cref="ArgumentException">Any of the disposables in the <paramref name="disposables"/> collection is <c>null</c>.</exception>
    public CompositeDisposable(IList<IDisposable> disposables)
    {
        if (disposables == null)

View on GitHub (pinned to 11c5427268)

Solutions

  1. Pass an actual array (possibly empty) instead of null: new CompositeDisposable(disposables ?? Array.Empty<IDisposable>()).
  2. Initialize disposable collections to empty rather than null.
  3. Use the IList overload or Add individually when the collection may be empty.

Example fix

// before
var cd = new CompositeDisposable(maybeNullArray);

// after
var cd = new CompositeDisposable(maybeNullArray ?? Array.Empty<IDisposable>());
Defensive patterns

Strategy: validation

Validate before calling

var arr = maybeNullArray ?? Array.Empty<IDisposable>();
var cd = new CompositeDisposable(arr);

Type guard

bool NotNull(IDisposable[]? a) => a is not null;

Prevention

When it happens

Trigger: Calling new CompositeDisposable((IDisposable[])null) or new CompositeDisposable(null) where overload resolution selects the params overload, or passing a null IDisposable[] variable.

Common situations: A collected array of disposables that ended up null because the source LINQ query returned null or was never initialized, then spread into the params constructor.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/dc205b2c6574f72c. Report an issue: GitHub.