{"record":{"id":"0e3ec230f052cac9","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-disposable-contextdisposable","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'disposable')","messagePattern":"Value cannot be null\\. \\(Parameter 'disposable'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Disposables/ContextDisposable.cs","lineNumber":26,"sourceCode":"namespace System.Reactive.Disposables\n{\n    /// <summary>\n    /// Represents a disposable resource whose disposal invocation will be posted to the specified <seealso cref=\"SynchronizationContext\"/>.\n    /// </summary>\n    public sealed class ContextDisposable : ICancelable\n    {\n        private volatile IDisposable _disposable;\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"ContextDisposable\"/> class that uses the specified <see cref=\"SynchronizationContext\"/> on which to dispose the specified disposable resource.\n        /// </summary>\n        /// <param name=\"context\">Context to perform disposal on.</param>\n        /// <param name=\"disposable\">Disposable whose Dispose operation to run on the given synchronization context.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"context\"/> or <paramref name=\"disposable\"/> is null.</exception>\n        public ContextDisposable(SynchronizationContext context, IDisposable disposable)\n        {\n            Context = context ?? throw new ArgumentNullException(nameof(context));\n            _disposable = disposable ?? throw new ArgumentNullException(nameof(disposable));\n        }\n\n        /// <summary>\n        /// Gets the provided <see cref=\"SynchronizationContext\"/>.\n        /// </summary>\n        public SynchronizationContext Context { get; }\n\n        /// <summary>\n        /// Gets a value that indicates whether the object is disposed.\n        /// </summary>\n        public bool IsDisposed => _disposable == BooleanDisposable.True;\n\n        /// <summary>\n        /// Disposes the underlying disposable on the provided <see cref=\"SynchronizationContext\"/>.\n        /// </summary>\n        public void Dispose()\n        {\n            var old = Interlocked.Exchange(ref _disposable, BooleanDisposable.True);","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Disposables/ContextDisposable.cs#L8-L44","documentation":"The ContextDisposable constructor throws ArgumentNullException when the disposable argument is null. The wrapped IDisposable is the action payload whose Dispose runs on the provided SynchronizationContext; a null wrapped resource cannot be dispatched. The constructor validates both parameters with ?? throw.","triggerScenarios":"new ContextDisposable(ctx, null), usually when the wrapped resource comes from a factory/lookup that returned null (failed acquisition of a stream, subscription, or handle).","commonSituations":"Wrapping resources obtained from methods that return null on failure instead of throwing; conditional resource creation where the null path was not handled before constructing the ContextDisposable.","solutions":["Check the resource for null before constructing; skip creation when null.","Substitute Disposable.Empty when a no-op disposable is acceptable: new ContextDisposable(ctx, Disposable.Empty).","Fix the resource factory so it throws a meaningful error instead of returning null."],"exampleFix":"// before\nvar d = new ContextDisposable(ctx, AcquireResource()); // may return null\n// after\nvar resource = AcquireResource();\nvar d = resource != null ? new ContextDisposable(ctx, resource) : null;","handlingStrategy":"validation","validationCode":"if (resource == null) return null; // or use Disposable.Empty\nvar d = new ContextDisposable(context, resource);","typeGuard":"ContextDisposable TryCreate(SynchronizationContext ctx, IDisposable d) => ctx == null || d == null ? null : new ContextDisposable(ctx, d);","tryCatchPattern":"try { var d = new ContextDisposable(context, resource); } catch (ArgumentNullException ex) when (ex.ParamName == \"disposable\") { /* handle missing resource */ }","preventionTips":["Treat factory methods returning null as failures before wrapping","Substitute Disposable.Empty for optional resources","Validate acquisition results before constructing wrappers"],"tags":["argument-null","constructor","disposable","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"}