{"record":{"id":"8b994fe5289b82b4","repo":"AvaloniaUI/Avalonia","slug":"value-cannot-be-null-parameter-item","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'item')","messagePattern":"Value cannot be null\\. \\(Parameter 'item'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Reactive/CompositeDisposable.cs","lineNumber":111,"sourceCode":"\n        return list;\n    }\n\n    /// <summary>\n    /// Gets the number of disposables contained in the <see cref=\"CompositeDisposable\"/>.\n    /// </summary>\n    public int Count => Volatile.Read(ref _count);\n\n    /// <summary>\n    /// Adds a disposable to the <see cref=\"CompositeDisposable\"/> or disposes the disposable if the <see cref=\"CompositeDisposable\"/> is disposed.\n    /// </summary>\n    /// <param name=\"item\">Disposable to add.</param>\n    /// <exception cref=\"ArgumentNullException\"><paramref name=\"item\"/> is <c>null</c>.</exception>\n    public void Add(IDisposable item)\n    {\n        if (item == null)\n        {\n            throw new ArgumentNullException(nameof(item));\n        }\n\n        lock (_gate)\n        {\n            if (!_disposed)\n            {\n                _disposables.Add(item);\n\n                // If read atomically outside the lock, it should be written atomically inside\n                // the plain read on _count is fine here because manipulation always happens\n                // from inside a lock.\n                Volatile.Write(ref _count, _count + 1);\n                return;\n            }\n        }\n\n        item.Dispose();\n    }","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Reactive/CompositeDisposable.cs#L93-L129","documentation":"CompositeDisposable.Add(item) appends a disposable to the composite (or disposes it immediately if the composite is already disposed). It throws ArgumentNullException(nameof(item)) when item is null, preserving the no-null-disposables invariant at the mutation boundary.","triggerScenarios":"Calling composite.Add(null), or Add-ing a disposable obtained from a subscription/factory that returned null.","commonSituations":"Adding a subscription token whose creation method returned null, or a conditional add where the disposable was not created.","solutions":["Null-check before Add: if (d is not null) composite.Add(d);.","Ensure subscription helpers always return a non-null IDisposable (use Disposable.Empty for no-ops).","Prefer the params/IList constructor for bulk adds so nulls surface at one validated point."],"exampleFix":"// before\ncomposite.Add(maybeNullDisposable);\n\n// after\nif (maybeNullDisposable is not null) composite.Add(maybeNullDisposable);","handlingStrategy":"validation","validationCode":"if (item is not null) composite.Add(item);","typeGuard":"bool IsDisposable(IDisposable? d) => d is not null;","tryCatchPattern":null,"preventionTips":["Guard Add with a null check.","Ensure subscription helpers return non-null disposables.","Use the validated bulk constructors for groups."],"tags":["avalonia","reactive","disposables","argumentnull","add"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}