{"record":{"id":"0d91c53d267ffc88","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-dispatcher-0d91c5","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.Linq/DispatcherObservable.cs","lineNumber":39,"sourceCode":"\n        /// <summary>\n        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"dispatcher\">Dispatcher whose associated message loop is used to notify observers on.</param>\n        /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"dispatcher\"/> is null.</exception>\n        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, Dispatcher dispatcher)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (dispatcher == null)\n            {\n                throw new ArgumentNullException(nameof(dispatcher));\n            }\n\n            return ObserveOn_(source, dispatcher);\n        }\n\n        /// <summary>\n        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"dispatcher\">Dispatcher whose associated message loop is used to notify observers on.</param>\n        /// <param name=\"priority\">Priority to schedule work items at.</param>\n        /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"dispatcher\"/> is null.</exception>\n        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, Dispatcher dispatcher, DispatcherPriority priority)\n        {\n            if (source == null)\n            {","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive.Wpf/System.Reactive.Linq/DispatcherObservable.cs#L21-L57","documentation":"ObserveOn(source, dispatcher) requires a non-null WPF Dispatcher to marshal observations onto the UI thread. When the dispatcher parameter is null the library throws ArgumentNullException because there is no target context to schedule notifications on. The check happens synchronously at call time, before subscription.","triggerScenarios":"Calling source.ObserveOn(dispatcher) with a null Dispatcher, e.g. Dispatcher == null on a DispatcherObject created off the UI thread, or caching a Dispatcher in a static that was never set in a non-WPF (console/unit-test) host.","commonSituations":"Accessing Application.Current.Dispatcher when Application.Current is null (unit tests, background service); a control created on a non-UI thread whose Dispatcher is null; DI container failing to inject a Dispatcher; running Rx.Wpf code in a headless environment.","solutions":["Pass a valid Dispatcher, typically Application.Current.Dispatcher or Dispatcher.CurrentDispatcher for the UI thread.","Guard with 'if (Application.Current != null)' or fall back to a non-dispatcher scheduler in headless/test hosts.","Ensure the control/DispatcherObject is created on a thread that runs a Dispatcher before using its Dispatcher."],"exampleFix":"// before\nvar onUi = source.ObserveOn(Application.Current.Dispatcher); // NRE if Application.Current is null\n// after\nvar dispatcher = Application.Current?.Dispatcher ?? Dispatcher.CurrentDispatcher;\nvar onUi = source.ObserveOn(dispatcher);","handlingStrategy":"validation","validationCode":"var dispatcher = Application.Current?.Dispatcher ?? Dispatcher.CurrentDispatcher; if (dispatcher == null) throw new InvalidOperationException(\"No dispatcher available in this host\");","typeGuard":"bool hasDispatcher = Application.Current != null && Application.Current.Dispatcher != null;","tryCatchPattern":"try { var ui = source.ObserveOn(dispatcher); } catch (ArgumentNullException ex) when (ex.ParamName == \"dispatcher\") { var ui = source.ObserveOn(Scheduler.Default); }","preventionTips":["Capture Application.Current.Dispatcher once at app startup.","Avoid using Dispatcher members in unit tests or headless hosts; abstract via IScheduler.","Check Application.Current for null before touching its Dispatcher."],"tags":["null-argument","wpf","dispatcher","reactive-extensions"],"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"}