{"record":{"id":"b347e4f9e7fa046a","repo":"dotnet/reactive","slug":"argumentnullexception-resultselector","errorCode":null,"errorMessage":"ArgumentNullException: resultSelector","messagePattern":"ArgumentNullException: resultSelector","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive.WindowsRuntime/System.Reactive.Linq/AsyncInfoObservable.cs","lineNumber":175,"sourceCode":"        /// Creates a Windows Runtime asynchronous operation that returns the last element of the result sequence, reporting incremental progress for each element produced by the source sequence.\n        /// Upon cancellation of the asynchronous operation, the subscription to the source sequence will be disposed.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <typeparam name=\"TResult\">The type of the elements in the result sequence.</typeparam>\n        /// <param name=\"source\">Source sequence to compute a result sequence that gets exposed as an asynchronous operation.</param>\n        /// <param name=\"resultSelector\">Selector function to map the source sequence on a result sequence.</param>\n        /// <returns>Windows Runtime asynchronous operation object that returns the last element of the result sequence, reporting incremental progress for each source sequence element.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"resultSelector\"/> is null.</exception>\n        public static IAsyncOperationWithProgress<TResult, int> ToAsyncOperationWithProgress<TSource, TResult>(this IObservable<TSource> source, Func<IObservable<TSource>, IObservable<TResult>> resultSelector)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (resultSelector == null)\n            {\n                throw new ArgumentNullException(nameof(resultSelector));\n            }\n\n            return AsyncInfo.Run<TResult, int>((ct, progress) =>\n            {\n                var i = 0;\n                return resultSelector(source.Do(_ => progress.Report(i++))).ToTask(ct);\n            });\n        }\n\n        /// <summary>\n        /// Creates a Windows Runtime asynchronous operation that returns the last element of the result sequence, using a selector function to map the source sequence on a progress reporting sequence.\n        /// Upon cancellation of the asynchronous operation, the subscription to the source sequence will be disposed.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <typeparam name=\"TResult\">The type of the elements in the result sequence.</typeparam>\n        /// <typeparam name=\"TProgress\">The type of the elements in the progress sequence.</typeparam>\n        /// <param name=\"source\">Source sequence to compute a result sequence that gets exposed as an asynchronous operation and a progress sequence that gets reported through the asynchronous operation.</param>\n        /// <param name=\"resultSelector\">Selector function to map the source sequence on a result sequence.</param>","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive.WindowsRuntime/System.Reactive.Linq/AsyncInfoObservable.cs#L157-L193","documentation":"ToAsyncOperationWithProgress also requires a non-null resultSelector — the function that maps the source observable to the result observable — and throws ArgumentNullException for 'resultSelector' when it is null. The selector defines the whole computation, so a null value would make the async operation non-functional.","triggerScenarios":"Calling source.ToAsyncOperationWithProgress(null), often when the selector is stored in a nullable Func field/variable that was never assigned, or when building the delegate conditionally.","commonSituations":"Dependency-injected selector strategies that defaulted to null, MVVM scenarios where the query factory is bound late, or typos passing the wrong variable (e.g. the progress selector) as the result selector.","solutions":["Pass a valid Func<IObservable<TSource>, IObservable<TResult>> as the second argument.","If the selector is computed, initialize it (e.g. xs => xs.LastAsync()) before invoking the method.","Guard with a null check and throw a descriptive error in your own code before calling."],"exampleFix":"// before\nFunc<IObservable<int>, IObservable<int>> selector = null;\nvar op = source.ToAsyncOperationWithProgress(selector);\n// after\nvar op = source.ToAsyncOperationWithProgress(xs => xs.LastAsync());","handlingStrategy":"validation","validationCode":"Func<IObservable<TSrc>, IObservable<TRes>> selector = xs => xs.LastAsync();\nif (selector is null) throw new InvalidOperationException(\"Selector not configured\");\nvar op = source.ToAsyncOperationWithProgress(selector);","typeGuard":"static bool IsValidSelector<TSrc, TRes>(Func<IObservable<TSrc>, IObservable<TRes>> f) => f is not null;","tryCatchPattern":"try\n{\n    var op = source.ToAsyncOperationWithProgress(selector);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"resultSelector\")\n{\n    log.Warn(\"resultSelector was null; check DI registration/argument order.\");\n}","preventionTips":["Pass lambdas inline (xs => ...) rather than via nullable Func variables.","Double-check argument order in multi-selector overloads.","Register selector delegates in DI with non-null defaults."],"tags":["null-check","argument-validation","rx","async"],"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"}