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
- Ensure the first disposable is created before composing.
- Substitute Disposable.Empty for the null element so the composite remains valid.
- 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
- Coalesce every optional disposable with Disposable.Empty before composing.
- Enable nullable reference types so null disposables surface at compile time.
- Keep resource acquisition methods non-null-returning.
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
- Value cannot be null. (Parameter 'disposable2')
- Value cannot be null. (Parameter 'disposables')
- Value cannot be null. (Parameter 'disposable')
- Value cannot be null. (Parameter 'disposable')
- RefCountDisposable
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)