{"record":{"id":"b0818b51fd39c066","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-compositedisposable","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'compositeDisposable')","messagePattern":"Value cannot be null\\. \\(Parameter 'compositeDisposable'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Disposables/Fluent/DisposableExtensions.cs","lineNumber":32,"sourceCode":"    /// </summary>\n    /// <typeparam name=\"T\">\n    /// The type of the disposable.\n    /// </typeparam>\n    /// <param name=\"item\">\n    /// The disposable we are going to want to be disposed by the CompositeDisposable.\n    /// </param>\n    /// <param name=\"compositeDisposable\">\n    /// The <see cref=\"CompositeDisposable\"/> to which <paramref name=\"item\"/> will be added.\n    /// </param>\n    /// <returns>\n    /// The disposable.\n    /// </returns>\n    public static T DisposeWith<T>(this T item, CompositeDisposable compositeDisposable)\n        where T : IDisposable\n    {\n        if (compositeDisposable == null)\n        {\n            throw new ArgumentNullException(nameof(compositeDisposable));\n        }\n\n        compositeDisposable.Add(item);\n        return item;\n    }\n}\n","sourceCodeStart":14,"sourceCodeEnd":39,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Disposables/Fluent/DisposableExtensions.cs#L14-L39","documentation":"The DisposeWith extension method throws ArgumentNullException when the compositeDisposable argument is null. DisposeWith adds the item to the given CompositeDisposable and returns it for fluent chaining; a null composite has nowhere to register the item, so it fails fast. Note the item itself may be null-tolerant in Add, but the composite cannot be null.","triggerScenarios":"someDisposable.DisposeWith(null), usually when the composite is a field/property that was not yet initialized (null before constructor wiring completes) or was set to null after disposal.","commonSituations":"Fluent subscription wiring in DI-scoped services where the CompositeDisposable is injected or created lazily; ordering bugs where DisposeWith runs before the composite field is assigned.","solutions":["Initialize the CompositeDisposable at field declaration: private readonly CompositeDisposable _disposables = new();","Ensure construction order assigns the composite before any DisposeWith calls.","Guard the call site: if (composite != null) item.DisposeWith(composite); or keep a non-null composite through the object's lifetime and Clear() instead of nulling it."],"exampleFix":"// before\nprivate CompositeDisposable _disposables; // assigned later\n...\nsubscription.DisposeWith(_disposables); // NRE/ ArgumentNullException\n// after\nprivate readonly CompositeDisposable _disposables = new CompositeDisposable();\n...\nsubscription.DisposeWith(_disposables);","handlingStrategy":"validation","validationCode":"if (composite == null) throw new InvalidOperationException(\"CompositeDisposable not initialized\");\nitem.DisposeWith(composite);","typeGuard":"bool CanDisposeWith<T>(T item, CompositeDisposable c) where T : IDisposable => item != null && c != null;","tryCatchPattern":"try { item.DisposeWith(composite); } catch (ArgumentNullException ex) when (ex.ParamName == \"compositeDisposable\") { /* log wiring failure */ }","preventionTips":["Declare CompositeDisposable fields readonly and initialize inline","Never null out the composite on Dispose; call Clear() instead","Ensure DisposeWith calls happen after dependency injection/constructor wiring"],"tags":["argument-null","extension-method","fluent","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-23T08:17:48.524Z"}