{"record":{"id":"27af2969d4488d15","repo":"dotnet/reactive","slug":"dependencyobject-parameter-dependencyobject","errorCode":null,"errorMessage":"dependencyObject (Parameter 'dependencyObject')","messagePattern":"dependencyObject \\(Parameter 'dependencyObject'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Linq/CoreDispatcherObservable.cs","lineNumber":88,"sourceCode":"#if IS_UAP\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=\"dependencyObject\">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=\"dependencyObject\"/> is null.</exception>\n        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (dependencyObject == null)\n            {\n                throw new ArgumentNullException(nameof(dependencyObject));\n            }\n\n            return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dependencyObject.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=\"dependencyObject\">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=\"dependencyObject\"/> is null.</exception>\n        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject, CoreDispatcherPriority priority)\n        {\n            if (source == null)\n            {","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Linq/CoreDispatcherObservable.cs#L70-L106","documentation":"To derive the dispatcher, this overload reads dependencyObject.Dispatcher; a null DependencyObject makes that impossible, so ArgumentNullException with ParamName 'dependencyObject' is thrown. Validation is eager at call time, before CoreDispatcherScheduler is created.","triggerScenarios":"Calling source.ObserveOn(null) with the DependencyObject overload, or passing an object whose resolution failed (e.g. FindName returned null, binding produced no target).","commonSituations":"FindName/VisualTreeHelper lookups returning null for elements not yet in the visual tree; passing a control from a template before it is applied; DI-resolved page/view being null in tests.","solutions":["Pass a real DependencyObject such as the page or control; verify the element exists in the visual tree first.","Resolve the element after Loaded/template-applied events rather than in the constructor.","If you only have a dispatcher, switch to the ObserveOn(CoreDispatcher) overload instead."],"exampleFix":"// before\nvar el = (FrameworkElement)tree.FindName(\"missing\");\nstream.ObserveOn(el); // throws when el is null\n// after\nvar el = (FrameworkElement)tree.FindName(\"target\");\nif (el != null) stream.ObserveOn(el).Subscribe(...);","handlingStrategy":"validation","validationCode":"var target = container.FindName(\"myControl\") as DependencyObject;\nif (target == null) return; // element not loaded yet; defer subscription","typeGuard":"bool HasDependencyObject(DependencyObject o) => o is not null;","tryCatchPattern":"try { stream.ObserveOn(dependencyObject).Subscribe(onNext); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"dependencyObject\") { /* defer to Loaded event and retry */ }","preventionTips":["Resolve controls after Loaded or OnApplyTemplate, not in constructors.","Check FindName/VisualTreeHelper results for null before use.","Prefer the CoreDispatcher overload when you already hold a dispatcher."],"tags":["argument-null","winrt","dependency-object","xaml"],"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"}