{"record":{"id":"d9f177c48a463e93","repo":"dotnet/reactive","slug":"argumentnullexception-source-coredispatcherobservable","errorCode":null,"errorMessage":"ArgumentNullException: source","messagePattern":"ArgumentNullException: source","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive.WindowsRuntime/System.Reactive.Linq/CoreDispatcherObservable.cs","lineNumber":34,"sourceCode":"    /// </summary>\n    [CLSCompliant(false)]\n    public static class CoreDispatcherObservable\n    {\n        #region ObserveOn[CoreDispatcher]\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        /// <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, CoreDispatcher 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 Synchronization.ObserveOn(source, new CoreDispatcherScheduler(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>","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive.WindowsRuntime/System.Reactive.Linq/CoreDispatcherObservable.cs#L16-L52","documentation":"ObserveOn(source, CoreDispatcher) marshals observable notifications onto a UI thread's dispatcher; the 'source' observable must be non-null and ArgumentNullException('source') is thrown otherwise. The check runs synchronously before any scheduling occurs.","triggerScenarios":"Calling nullSource.ObserveOn(dispatcher) in a UWP/WinRT app, typically when the observable came from a nullable field, a failed service resolution, or an optional event stream.","commonSituations":"ViewModels subscribing to streams that were only initialized on certain pages, or code that moved between WPF (DispatcherScheduler) and UWP (CoreDispatcher) and broke initialization.","solutions":["Ensure the observable is created before calling ObserveOn; use Observable.Defer to create it lazily.","Fix the upstream factory that returned null instead of an observable.","Subscribe on a fallback observable (e.g. Observable.Empty) when the stream is legitimately absent."],"exampleFix":"// before\nIObservable<EventData> stream = null;\nvar ui = stream.ObserveOn(Dispatcher);\n// after\nIObservable<EventData> stream = eventService.Streams; // must be non-null\nvar ui = stream.ObserveOn(Dispatcher);","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\nif (dispatcher is null) throw new ArgumentNullException(nameof(dispatcher));\nvar ui = source.ObserveOn(dispatcher);","typeGuard":"static bool CanObserveOn<T>(IObservable<T> s, CoreDispatcher d) => s is not null && d is not null;","tryCatchPattern":"try\n{\n    var ui = source.ObserveOn(dispatcher);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    log.Warn(\"Stream was null before ObserveOn; check upstream initialization.\");\n}","preventionTips":["Initialize observable streams before UI-thread marshaling.","Keep stream producers non-null-returning.","Capture dispatcher and stream together in one non-null bundle (e.g. a record)."],"tags":["null-check","argument-validation","rx","uwp"],"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"}