{"record":{"id":"fdf7e4732c7fe76f","repo":"dotnet/reactive","slug":"argumentnullexception-source-windowsobservable-events","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/WindowsObservable.Events.cs","lineNumber":114,"sourceCode":"                {\n                    removeHandler(h);\n                };\n            });\n        }\n\n        /// <summary>\n        /// Exposes an observable sequence as an object with a typed event.\n        /// </summary>\n        /// <typeparam name=\"TSender\">The type of the sender that raises the event.</typeparam>\n        /// <typeparam name=\"TEventArgs\">The type of the event data generated by the event.</typeparam>\n        /// <param name=\"source\">Observable source sequence.</param>\n        /// <returns>The event source object.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        public static IEventPatternSource<TSender, TEventArgs> ToEventPattern<TSender, TEventArgs>(this IObservable<EventPattern<TSender, TEventArgs>> source)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            return new EventPatternSource<TSender, TEventArgs>(source, static (h, evt) => h(evt.Sender!, evt.EventArgs));\n        }\n    }\n}\n","sourceCodeStart":96,"sourceCodeEnd":121,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive.WindowsRuntime/System.Reactive.Linq/WindowsObservable.Events.cs#L96-L121","documentation":"ToEventPattern converts an IObservable of EventPattern back into a .NET-style event source, and it throws ArgumentNullException when the source observable is null. Because the entire conversion is driven by the source sequence, a null source cannot produce a meaningful result. The check is eager so the failure surfaces at the call site.","triggerScenarios":"Calling source.ToEventPattern<TSender, TEventArgs>() where source is a null IObservable<EventPattern<TSender, TEventArgs>>.","commonSituations":"A factory method or dictionary lookup returned a null observable; a conditional query pipeline short-circuited to null instead of Observable.Empty; refactoring changed a field to nullable without updating call sites.","solutions":["Ensure the observable passed to ToEventPattern is non-null before calling.","If a producer can yield null, substitute Observable.Empty<EventPattern<TSender, TEventArgs>>() or the ?? operator.","Move the ToEventPattern call inside the success branch where the source is known to exist."],"exampleFix":"// before\nIEventPatternSource<object, RoutedEventArgs> src = maybeObservable.ToEventPattern<object, RoutedEventArgs>(); // maybeObservable may be null\n// after\nIEventPatternSource<object, RoutedEventArgs> src = (maybeObservable ?? Observable.Empty<EventPattern<object, RoutedEventArgs>>())\n    .ToEventPattern<object, RoutedEventArgs>();","handlingStrategy":"validation","validationCode":"if (source is null) source = Observable.Empty<EventPattern<TSender, TEventArgs>>();","typeGuard":"static bool IsObservableValid<TSender, TArgs>(IObservable<EventPattern<TSender, TArgs>>? s) => s is not null;","tryCatchPattern":"try { var src = observable.ToEventPattern<TSender, TEventArgs>(); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* use default event source */ }","preventionTips":["Adopt the convention of never returning null observables; return Observable.Empty instead.","Apply the ?? operator with Observable.Empty when a source may be absent.","Enable nullable reference types on the project."],"tags":["argument-null","reactive-extensions","uwp","observable"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}