{"record":{"id":"e4a053f42e4ede4c","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-dispatcherobject","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'dispatcherObject')","messagePattern":"Value cannot be null\\. \\(Parameter 'dispatcherObject'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive.Wpf/System.Reactive.Linq/DispatcherObservable.cs","lineNumber":110,"sourceCode":"\n        /// <summary>\n        /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.\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=\"dispatcherObject\">Object to get the dispatcher from.</param>\n        /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"dispatcherObject\"/> is null.</exception>\n        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DispatcherObject dispatcherObject)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (dispatcherObject == null)\n            {\n                throw new ArgumentNullException(nameof(dispatcherObject));\n            }\n\n            return ObserveOn_(source, dispatcherObject.Dispatcher);\n        }\n\n        /// <summary>\n        /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.\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=\"dispatcherObject\">Object to get the dispatcher from.</param>\n        /// <param name=\"priority\">Priority to schedule work items at.</param>\n        /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"dispatcherObject\"/> is null.</exception>\n        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DispatcherObject dispatcherObject, DispatcherPriority priority)\n        {\n            if (source == null)\n            {","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive.Wpf/System.Reactive.Linq/DispatcherObservable.cs#L92-L128","documentation":"This ObserveOn overload validates the DispatcherObject parameter and throws ArgumentNullException('dispatcherObject') when null. The object's Dispatcher is accessed immediately (dispatcherObject.Dispatcher), so a null control/window reference fails fast with a clear parameter name.","triggerScenarios":"Calling source.ObserveOn(control) where control is null: DataContext not yet bound, FindName/FindResource returning null, a window not yet loaded, or a control reference cleared after disposal.","commonSituations":"XAML name resolution timing (event handlers firing before Loaded); controls removed from the visual tree; templated parts not yet applied when a stream subscription is set up; code-behind running before InitializeComponent completes.","solutions":["Defer the subscription until the control/window exists: subscribe in Loaded/Initialized handlers instead of constructors.","Use Application.Current.Dispatcher (or the Window's Dispatcher) as a stable target instead of a possibly-null control reference.","Guard with a null check on the control and fall back to the current Dispatcher."],"exampleFix":"// before\nvar ui = source.ObserveOn(this.someControl); // someControl null pre-Load\nvar ui2 = ui.Subscribe(x => ...);\n// after\nvar dispatcher = this.someControl?.Dispatcher ?? Application.Current.Dispatcher;\nvar ui = source.ObserveOn(dispatcher);\nvar ui2 = ui.Subscribe(x => ...);","handlingStrategy":"validation","validationCode":"if (dispatcherObject == null) dispatcherObject = Application.Current?.MainWindow; // plus fall back to dispatcher directly","typeGuard":"bool controlReady = dispatcherObject != null && dispatcherObject.Dispatcher != null;","tryCatchPattern":"try { var ui = source.ObserveOn(dispatcherObject); } catch (ArgumentNullException ex) when (ex.ParamName == \"dispatcherObject\") { var ui = source.ObserveOn(Application.Current.Dispatcher); }","preventionTips":["Subscribe in Loaded/OnApplyTemplate rather than constructors, so controls exist.","Prefer passing a Dispatcher directly over a control reference when the control may be transient.","Null-check controls obtained via FindName or GetTemplateChild before use."],"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"}