{"record":{"id":"bdf8d8263da05cb1","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-disposable2","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'disposable2')","messagePattern":"Value cannot be null\\. \\(Parameter 'disposable2'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Disposables/StableCompositeAsyncDisposable.cs","lineNumber":18,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Collections.Generic;\nusing System.Threading;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Disposables\n{\n    public abstract class StableCompositeAsyncDisposable : IAsyncDisposable\n    {\n        public static StableCompositeAsyncDisposable Create(IAsyncDisposable disposable1, IAsyncDisposable disposable2)\n        {\n            if (disposable1 == null)\n                throw new ArgumentNullException(nameof(disposable1));\n            if (disposable2 == null)\n                throw new ArgumentNullException(nameof(disposable2));\n\n            return new Binary(disposable1, disposable2);\n        }\n\n        public static StableCompositeAsyncDisposable Create(params IAsyncDisposable[] disposables)\n        {\n            if (disposables == null)\n                throw new ArgumentNullException(nameof(disposables));\n\n            return new NAry(disposables);\n        }\n\n        public static StableCompositeAsyncDisposable Create(IEnumerable<IAsyncDisposable> disposables)\n        {\n            if (disposables == null)\n                throw new ArgumentNullException(nameof(disposables));\n\n            return new NAry(disposables);","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Disposables/StableCompositeAsyncDisposable.cs#L1-L36","documentation":"StableCompositeAsyncDisposable.Create(disposable1, disposable2) validates both arguments before constructing a Binary composite. The second IAsyncDisposable was null, so the library throws ArgumentNullException immediately rather than failing at disposal time.","triggerScenarios":"Calling StableCompositeAsyncDisposable.Create(someDisposable, null) — null second argument, e.g. a subscription or stream that was never created.","commonSituations":"Conditional subscription logic (\"only subscribe if enabled\") that leaves the second disposable null; a library API returning null on failure; typos passing the wrong variable.","solutions":["Pass a non-null IAsyncDisposable as the second argument.","Fix the producer of disposable2 so it returns a real disposable or a null-object instead of null.","Collect only non-null disposables and use the params or IEnumerable Create overload instead."],"exampleFix":"// before\nvar composite = StableCompositeAsyncDisposable.Create(conn, maybeSub);\n// after\nvar disposables = new List<IAsyncDisposable> { conn };\nif (maybeSub != null) disposables.Add(maybeSub);\nvar composite = StableCompositeAsyncDisposable.Create(disposables);","handlingStrategy":"validation","validationCode":"if (disposable2 is null) throw new ArgumentNullException(nameof(disposable2));\nvar composite = StableCompositeAsyncDisposable.Create(disposable1, disposable2);","typeGuard":"static bool IsDisposable(IAsyncDisposable? d) => d is not null;","tryCatchPattern":"try { var c = StableCompositeAsyncDisposable.Create(d1, d2); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"disposable2\")\n{ log.LogWarning(\"disposable2 was null\"); }","preventionTips":["Initialize disposable fields at declaration so they are never null.","Skip composition entirely when an optional resource was not created.","Use collection overloads with non-null items instead of positional pairs with nulls."],"tags":["argument-null","disposables","async-rx"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}