{"record":{"id":"9b0a78048d357879","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-action-dispatcherscheduler","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'action')","messagePattern":"Value cannot be null\\. \\(Parameter 'action'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Concurrency/DispatcherScheduler.cs","lineNumber":84,"sourceCode":"\n        /// <summary>\n        /// Gets the priority at which work items will be dispatched.\n        /// </summary>\n        public System.Windows.Threading.DispatcherPriority Priority { get; }\n\n        /// <summary>\n        /// Schedules an action to be executed on the dispatcher.\n        /// </summary>\n        /// <typeparam name=\"TState\">The type of the state passed to the scheduled action.</typeparam>\n        /// <param name=\"state\">State passed to the action to be executed.</param>\n        /// <param name=\"action\">Action to be executed.</param>\n        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"action\"/> is <c>null</c>.</exception>\n        public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)\n        {\n            if (action == null)\n            {\n                throw new ArgumentNullException(nameof(action));\n            }\n\n            var d = new SingleAssignmentDisposable();\n\n            Dispatcher.BeginInvoke(\n                new Action(() =>\n                {\n                    if (!d.IsDisposed)\n                    {\n                        d.Disposable = action(this, state);\n                    }\n                }),\n                Priority\n            );\n\n            return d;\n        }\n","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Concurrency/DispatcherScheduler.cs#L66-L102","documentation":"DispatcherScheduler.Schedule validates its action delegate before dispatching work to the WPF Dispatcher. A null action would fail later inside the dispatcher callback with no useful stack trace, so the library fails fast with ArgumentNullException. This is a caller programming error, not an environmental issue.","triggerScenarios":"Calling scheduler.Schedule(state, (sched, st) => ...) with a null delegate, e.g. var d = new DispatcherScheduler(d).Schedule(state, null); or passing a null delegate variable that was expected to be assigned.","commonSituations":"Building a wrapper that conditionally assigns the action delegate but leaves it null on some path; storing the action in a field initialized to null; refactoring code that used to pass a lambda into one that passes a nullable Func field.","solutions":["Ensure a non-null Func<IScheduler, TState, IDisposable> is passed to Schedule.","Check that any delegate variable/field is initialized before the Schedule call.","Guard at the call site: if (action != null) scheduler.Schedule(state, action);"],"exampleFix":"// before\nscheduler.Schedule(state, null);\n// after\nIDisposable d = scheduler.Schedule(state, (sched, st) => { DoWork(st); return Disposable.Empty; });","handlingStrategy":"validation","validationCode":"if (action == null) throw new InvalidOperationException(\"Schedule called before action was assigned\");\nvar d = dispatcherScheduler.Schedule(state, action);","typeGuard":"static bool HasAction<TState>(Func<IScheduler, TState, IDisposable> action) => action is not null;","tryCatchPattern":"try { var d = scheduler.Schedule(state, action); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"action\") { log.Error(\"No action supplied to scheduler\"); }","preventionTips":["Initialize delegate fields at construction time, not lazily","Never store schedulable actions in nullable fields without assignment checks","Wrap scheduler calls in a helper that validates arguments once"],"tags":["argument-null","scheduler","wpf"],"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"}