{"record":{"id":"a237f8b53dc15c4e","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-dispatcher-a237f8","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'dispatcher')","messagePattern":"Value cannot be null\\. \\(Parameter 'dispatcher'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive.Wpf/System.Reactive.Concurrency/DispatcherScheduler.cs","lineNumber":48,"sourceCode":"        /// </summary>\n        public static DispatcherScheduler Current\n        {\n            get\n            {\n                var dispatcher = System.Windows.Threading.Dispatcher.FromThread(Thread.CurrentThread)\n                    ?? throw new InvalidOperationException(Strings_Wpf_WindowsThreading.NO_DISPATCHER_CURRENT_THREAD);\n                return new DispatcherScheduler(dispatcher);\n            }\n        }\n\n        /// <summary>\n        /// Constructs a <see cref=\"DispatcherScheduler\"/> that schedules units of work on the given <see cref=\"System.Windows.Threading.Dispatcher\"/>.\n        /// </summary>\n        /// <param name=\"dispatcher\"><see cref=\"DispatcherScheduler\"/> to schedule work on.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"dispatcher\"/> is <c>null</c>.</exception>\n        public DispatcherScheduler(System.Windows.Threading.Dispatcher dispatcher)\n        {\n            Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));\n            Priority = System.Windows.Threading.DispatcherPriority.Normal;\n\n        }\n\n        /// <summary>\n        /// Constructs a <see cref=\"DispatcherScheduler\"/> that schedules units of work on the given <see cref=\"System.Windows.Threading.Dispatcher\"/> at the given priority.\n        /// </summary>\n        /// <param name=\"dispatcher\"><see cref=\"DispatcherScheduler\"/> to schedule work on.</param>\n        /// <param name=\"priority\">Priority at which units of work are scheduled.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"dispatcher\"/> is <c>null</c>.</exception>\n        public DispatcherScheduler(System.Windows.Threading.Dispatcher dispatcher, System.Windows.Threading.DispatcherPriority priority)\n        {\n            Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));\n            Priority = priority;\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"System.Windows.Threading.Dispatcher\"/> associated with the <see cref=\"DispatcherScheduler\"/>.","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive.Wpf/System.Reactive.Concurrency/DispatcherScheduler.cs#L30-L66","documentation":"The DispatcherScheduler constructor validates its dispatcher parameter and throws ArgumentNullException if null. A scheduler without a Dispatcher cannot schedule any work, so the library rejects it immediately.","triggerScenarios":"new DispatcherScheduler(null), typically from a property/field such as SomeElement.Dispatcher read before the element is attached to the visual tree, or a Dispatcher-returning helper returning null.","commonSituations":"Reading control.Dispatcher in a constructor before WPF assigns it; DI-injected dispatcher that was never registered; using Dispatcher.FromThread on a non-UI thread (returns null) and feeding the result into the constructor.","solutions":["Pass a valid dispatcher, e.g. Application.Current.Dispatcher or theWindow.Dispatcher.","Ensure the control is constructed/loaded before reading its Dispatcher property.","Null-check the dispatcher source before constructing the scheduler and fall back to Application.Current.Dispatcher."],"exampleFix":"// before\nvar sched = new DispatcherScheduler(myControl.Dispatcher); // may be null if control not loaded\n// after\nvar dispatcher = myControl?.Dispatcher ?? Application.Current?.Dispatcher ?? throw new InvalidOperationException(\"no UI dispatcher available\");\nvar sched = new DispatcherScheduler(dispatcher);","handlingStrategy":"validation","validationCode":"if (dispatcher == null) dispatcher = Application.Current?.Dispatcher ?? throw new InvalidOperationException(\"no UI dispatcher\");","typeGuard":"bool IsValidDispatcher(System.Windows.Threading.Dispatcher d) => d is not null;","tryCatchPattern":"try { var sched = new DispatcherScheduler(dispatcher); } catch (ArgumentNullException ex) when (ex.ParamName == \"dispatcher\") { /* resolve from Application.Current */ }","preventionTips":["Prefer Application.Current.Dispatcher over per-control Dispatcher lookups.","Only read element.Dispatcher after the element is constructed on the UI thread.","Centralize dispatcher resolution in one helper that never returns null."],"tags":["wpf","dispatcher","null-argument","scheduler"],"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"}