{"record":{"id":"052c4c6b6d923f9d","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-dispose-disposable","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'dispose')","messagePattern":"Value cannot be null\\. \\(Parameter 'dispose'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Disposables/Disposable.cs","lineNumber":52,"sourceCode":"            }\n        }\n\n        /// <summary>\n        /// Gets the disposable that does nothing when disposed.\n        /// </summary>\n        public static IDisposable Empty => EmptyDisposable.Instance;\n\n        /// <summary>\n        /// Creates a disposable object that invokes the specified action when disposed.\n        /// </summary>\n        /// <param name=\"dispose\">Action to run during the first call to <see cref=\"IDisposable.Dispose\"/>. The action is guaranteed to be run at most once.</param>\n        /// <returns>The disposable object that runs the given action upon disposal.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"dispose\"/> is <c>null</c>.</exception>\n        public static IDisposable Create(Action dispose)\n        {\n            if (dispose == null)\n            {\n                throw new ArgumentNullException(nameof(dispose));\n            }\n\n            return new AnonymousDisposable(dispose);\n        }\n\n        /// <summary>\n        /// Creates a disposable object that invokes the specified action when disposed.\n        /// </summary>\n        /// <param name=\"state\">The state to be passed to the action.</param>\n        /// <param name=\"dispose\">Action to run during the first call to <see cref=\"IDisposable.Dispose\"/>. The action is guaranteed to be run at most once.</param>\n        /// <returns>The disposable object that runs the given action upon disposal.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"dispose\"/> is <c>null</c>.</exception>\n        public static IDisposable Create<TState>(TState state, Action<TState> dispose)\n        {\n            if (dispose == null)\n            {\n                throw new ArgumentNullException(nameof(dispose));\n            }","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Disposables/Disposable.cs#L34-L70","documentation":"Disposable.Create(Action dispose) throws ArgumentNullException when the dispose action is null. Create builds an AnonymousDisposable that invokes the given action on disposal; a null action would make disposal a no-op silently or crash later, so the factory validates eagerly. The contract is documented in the method's XML docs.","triggerScenarios":"Disposable.Create(null), often when the action is built from a delegate field/parameter that is null (unassigned callback, optional hook), or from a ternary/LINQ selection that produced null.","commonSituations":"Registering cleanup callbacks where the cleanup delegate is conditionally assigned; passing a method group resolved via reflection or a dictionary of handlers that missed the key.","solutions":["Provide a real action; use Disposable.Empty if no cleanup is needed.","Null-check the delegate before calling Create and fall back to Disposable.Empty.","Ensure the callback field is initialized at declaration (e.g. = () => {}) rather than left null."],"exampleFix":"// before\nvar d = Disposable.Create(_cleanup); // _cleanup may be null\n// after\nvar d = _cleanup != null ? Disposable.Create(_cleanup) : Disposable.Empty;","handlingStrategy":"validation","validationCode":"if (cleanup == null) return Disposable.Empty;\nvar d = Disposable.Create(cleanup);","typeGuard":"IDisposable TryCreate(Action cleanup) => cleanup == null ? Disposable.Empty : Disposable.Create(cleanup);","tryCatchPattern":"try { var d = Disposable.Create(dispose); } catch (ArgumentNullException ex) when (ex.ParamName == \"dispose\") { d = Disposable.Empty; }","preventionTips":["Initialize delegate fields at declaration","Map 'no cleanup needed' to Disposable.Empty, not null","Null-check optional callbacks before registering them"],"tags":["argument-null","factory","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"}