dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'disposable1')

Error message

Value cannot be null. (Parameter 'disposable1')

What it means

StableCompositeDisposable.Create(IDisposable, IDisposable) validates both arguments and throws ArgumentNullException when the first is null. A composite must be built only from real disposables; a null element would break disposal behavior, so it is rejected up front.

Solutions

  1. Ensure the first disposable is created before composing.
  2. Substitute Disposable.Empty for the null element so the composite remains valid.
  3. Null-check and fall back to composing only the non-null disposables.

Example fix

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

Strategy: validation

Validate before calling

if (first is null) first = 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 == "disposable1") { /* compose remaining or use Empty */ }

Prevention

When it happens

Trigger: Calling StableCompositeDisposable.Create(null, d2) — first argument null, e.g. a first subscription/resource that was null.

Common situations: Combining two resources where one acquisition returned null, event-driven code where one of the resources is optional, or LINQ/conditional setup leaving a variable unassigned.

Related errors


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

Appendix: source

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

namespace System.Reactive.Disposables
{
    /// <summary>
    /// Represents a group of disposable resources that are disposed together.
    /// </summary>
#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)

View on GitHub (pinned to 94b5d5ab91)