{"record":{"id":"e360fba7e65f81b4","repo":"dotnet/reactive","slug":"dependencyobject","errorCode":null,"errorMessage":"dependencyObject","messagePattern":"dependencyObject","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Linq/CoreDispatcherObservable.cs","lineNumber":234,"sourceCode":"        /// <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 subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"dependencyObject\"/> is null.</exception>\n        /// <remarks>\n        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the specified object.\n        /// In order to invoke observer callbacks on the dispatcher associated with the specified object, e.g. to render results in a control, use <see cref=\"CoreDispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, DependencyObject)\"/>.\n        /// </remarks>\n        public static IObservable<TSource> SubscribeOn<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.SubscribeOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher));\n        }\n\n        /// <summary>\n        /// Wraps the source sequence in order to run its subscription and unsubscription logic 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 subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"dependencyObject\"/> is null.</exception>\n        /// <remarks>\n        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the specified object.\n        /// In order to invoke observer callbacks on the dispatcher associated with the specified object, e.g. to render results in a control, use <see cref=\"CoreDispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, DependencyObject, CoreDispatcherPriority)\"/>.\n        /// </remarks>","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Linq/CoreDispatcherObservable.cs#L216-L252","documentation":"SubscribeOn(source, dependencyObject) throws ArgumentNullException when the dependencyObject parameter is null. The method needs it solely to read dependencyObject.Dispatcher and construct a CoreDispatcherScheduler. A null element therefore cannot provide a subscription target, and the library fails fast at composition time.","triggerScenarios":"Passing a null DependencyObject — e.g. a control resolved via FindName that does not exist in the visual tree yet, a Content binding that is null at pipeline-construction time, or a page/element reference taken after unloading.","commonSituations":"Building pipelines in a control's constructor before the visual tree is loaded; OnNavigatedFrom cleanup running after the element was released; ItemTemplate elements whose DataContext resolves to null during virtualization.","solutions":["Defer the call until the element is loaded (hook Loaded/Frame.Loaded and build the pipeline there).","Pass a CoreDispatcher captured earlier instead of a DependencyObject: SubscribeOn(source, dispatcher).","Null-check the element and fall back to CoreDispatcherScheduler.Current.Dispatcher captured on the UI thread.","If the element legitimately may be absent, skip applying SubscribeOn rather than passing null."],"exampleFix":"// before\nvar target = (Panel)this.FindName(\"ResultsPanel\"); // null if name missing\nstream.SubscribeOn(target).Subscribe(...);\n// after\nvar target = (Panel)this.FindName(\"ResultsPanel\");\nif (target != null) stream.SubscribeOn(target).Subscribe(...);\nelse stream.SubscribeOn(App.UiDispatcher).Subscribe(...);","handlingStrategy":"validation","validationCode":"var target = this.FindName(\"ResultsPanel\") as DependencyObject;\nif (target != null) source = source.SubscribeOn(target);\nelse source = source.SubscribeOn(App.UiDispatcher);","typeGuard":"bool HasValidTarget(DependencyObject o) => o != null && o.Dispatcher != null;","tryCatchPattern":"try { source.SubscribeOn(dependencyObject).Subscribe(handler); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"dependencyObject\")\n{ source.SubscribeOn(App.UiDispatcher).Subscribe(handler); }","preventionTips":["Build element-bound pipelines inside the Loaded event handler.","Check FindName/x:Bind results for null before use.","Capture the dispatcher instead of holding element references that can be released.","Account for list virtualization: item elements may be null when the pipeline is built."],"tags":["argumentnull","winrt","reactive-extensions","dependencyobject","null-element"],"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"}