{"record":{"id":"9517bc1afe93b7e5","repo":"AvaloniaUI/Avalonia","slug":"value-cannot-be-null-parameter-compositedisposa","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":"src/Avalonia.Base/Reactive/DisposableMixin.cs","lineNumber":31,"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 is null)\n        {\n            throw new ArgumentNullException(nameof(compositeDisposable));\n        }\n\n        compositeDisposable.Add(item);\n        return item;\n    }\n}\n","sourceCodeStart":13,"sourceCodeEnd":38,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Reactive/DisposableMixin.cs#L13-L38","documentation":"Thrown by the DisposeWith(this T item, CompositeDisposable compositeDisposable) extension when compositeDisposable is null. DisposeWith is a fluent convenience that registers an IDisposable into a CompositeDisposable so the whole group can be torn down together. A null composite would silently drop the registration and leak the item, so it is rejected up front.","triggerScenarios":"Calling someDisposable.DisposeWith(null) or DisposeWith(_disposables) where _disposables has not been initialized or was already disposed and nulled out.","commonSituations":"Field initialization order: DisposeWith is called in a constructor before the CompositeDisposable field is assigned; the composite was disposed and the field set to null in cleanup code; a null composite is passed from a property whose backing field is lazy-initialized but not yet accessed.","solutions":["Initialize the CompositeDisposable field/property before any DisposeWith call (assign it inline or in the constructor before subscriptions).","Null-check the composite at the call site and skip DisposeWith if null, or create one on demand.","Stop nulling out the composite on dispose — dispose it but keep the reference, or stop subscribing after disposal."],"exampleFix":"// before\nprivate CompositeDisposable _disposables; // never assigned\nsub.Subscribe(handler).DisposeWith(_disposables);\n// after\nprivate readonly CompositeDisposable _disposables = new();\nsub.Subscribe(handler).DisposeWith(_disposables);","handlingStrategy":"validation","validationCode":"if (compositeDisposable is null) throw new ArgumentNullException(nameof(compositeDisposable));\nitem.DisposeWith(compositeDisposable);","typeGuard":"static bool IsUsable(CompositeDisposable? cd) => cd is not null;","tryCatchPattern":null,"preventionTips":["Initialize CompositeDisposable fields inline or in the ctor before subscribing.","Do not null the field on dispose; dispose the instance and guard later subscriptions with a disposed flag.","Unit-test subscription ordering to catch uninitialized composites."],"tags":["reactive","null-argument","composite-disposable","lifecycle"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}