dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'disposable2')

Error message

Value cannot be null. (Parameter 'disposable2')

What it means

StableCompositeDisposable.Create(IDisposable, IDisposable) throws ArgumentNullException when the second argument is null. Each of the two-argument overload's parameters is validated independently, so this fires only when disposable2 is null.

Solutions

  1. Ensure the second disposable is created before composing.
  2. Substitute Disposable.Empty for the null element.
  3. Null-check and compose only the non-null disposables.

Example fix

// before
var composite = StableCompositeDisposable.Create(first, second); // second is null
// after
var composite = StableCompositeDisposable.Create(first, second ?? Disposable.Empty);
Defensive patterns

Strategy: validation

Validate before calling

if (second is null) second = Disposable.Empty;
var composite = StableCompositeDisposable.Create(first, second);

Type guard

static bool IsValidPair(IDisposable a, IDisposable b) => a is not null && b is not null;

Try / catch

try { var c = StableCompositeDisposable.Create(first, second); }
catch (ArgumentNullException ex) when (ex.ParamName == "disposable2") { /* compose remaining or use Empty */ }

Prevention

When it happens

Trigger: Calling StableCompositeDisposable.Create(d1, null) — second argument null, e.g. the second resource acquisition returned null.

Common situations: Optional second subscription (e.g. a timer or background resource that was never created), or refactoring that made one of the two sources nullable.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/e5c4c53a8e246e9d. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Disposables/StableCompositeDisposable.cs:31

#pragma warning disable CA1063 // (Overridable IDisposable.) This analyzer wants us to make breaking changes to its public API, which we can't do.
    public abstract class StableCompositeDisposable : ICancelable
    {
        /// <summary>
        /// Creates a new group containing two disposable resources that are disposed together.
        /// </summary>
        /// <param name="disposable1">The first disposable resource to add to the group.</param>
        /// <param name="disposable2">The second disposable resource to add to the group.</param>
        /// <returns>Group of disposable resources that are disposed together.</returns>
        public static ICancelable Create(IDisposable disposable1, IDisposable disposable2)
        {
            if (disposable1 == null)
            {
                throw new ArgumentNullException(nameof(disposable1));
            }

            if (disposable2 == null)
            {
                throw new ArgumentNullException(nameof(disposable2));
            }

            return new Binary(disposable1, disposable2);
        }

        /// <summary>
        /// Creates a new group of disposable resources that are disposed together.
        /// </summary>
        /// <param name="disposables">Disposable resources to add to the group.</param>
        /// <returns>Group of disposable resources that are disposed together.</returns>
        public static ICancelable Create(params IDisposable[] disposables)
        {
            if (disposables == null)
            {
                throw new ArgumentNullException(nameof(disposables));
            }

            return new NAryArray(disposables);

View on GitHub (pinned to 94b5d5ab91)