{"record":{"id":"171d9d2d505c2ce6","repo":"dotnet/reactive","slug":"argument-cannot-be-null-parameter-name-resultselector","errorCode":null,"errorMessage":"Argument cannot be null (Parameter name: resultSelector)","messagePattern":"Argument cannot be null \\(Parameter name: resultSelector\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/For.cs","lineNumber":21,"sourceCode":"// See the LICENSE file in the project root for more information. \n\nusing System.Collections.Generic;\nusing System.Reactive.Disposables;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        // REVIEW: Use a tail-recursive sink.\n        // TODO: Add IAsyncEnumerable<T> based overlaod.\n\n        public static IAsyncObservable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IAsyncObservable<TResult>> resultSelector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n\n            return For(source, x => new ValueTask<IAsyncObservable<TResult>>(resultSelector(x)));\n        }\n\n        public static IAsyncObservable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, ValueTask<IAsyncObservable<TResult>>> resultSelector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n\n            return Create<TResult>(async observer =>\n            {\n                var subscription = new SerialAsyncDisposable();\n\n                var enumerator = source.GetEnumerator();\n\n                var o = default(IAsyncObserver<TResult>);","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/For.cs#L3-L39","documentation":"For(IEnumerable<TSource>, Func<TSource,IAsyncObservable<TResult>>) throws ArgumentNullException because the resultSelector delegate is null. For each element of the source, resultSelector must produce an IAsyncObservable<TResult>; the operator validates it at creation time and names 'resultSelector' in the exception. A null selector cannot fall back to any default behavior.","triggerScenarios":"Calling AsyncObservable.For(source, (Func<TSource,IAsyncObservable<TResult>>)null) — usually a null local, a conditional delegate assignment that skipped assignment, or forwarding an optional selector parameter left null.","commonSituations":"Factories that build the selector from configuration and return null when config is missing; refactoring that renamed a method and the method-group reference was lost; optional parameters defaulting to null.","solutions":["Pass a valid selector, e.g. x => AsyncObservable.Return(Transform(x)).","If the selector is optional, substitute a default: resultSelector ?? (x => AsyncObservable.Return(x)).","Fix the factory/config path so it always yields a delegate or short-circuit to Empty when absent.","Validate the selector at the API boundary before composing the pipeline."],"exampleFix":"// before\nFunc<string, IAsyncObservable<int>> selector = config.Selector; // null\nvar result = AsyncObservable.For(names, selector);\n\n// after\nvar result = AsyncObservable.For(names, config.Selector ?? (x => AsyncObservable.Return(x.Length)));","handlingStrategy":"validation","validationCode":"if (resultSelector == null)\n    throw new InvalidOperationException(\"For requires a result selector; provide one or use Empty.\");\nvar result = AsyncObservable.For(source, resultSelector);","typeGuard":"static bool IsValidSelector<T, R>(Func<T, IAsyncObservable<R>> s) => s is not null;","tryCatchPattern":"try { var result = AsyncObservable.For(source, selector); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"resultSelector\") { /* supply default selector or fail with config error */ }","preventionTips":["Coalesce optional selectors: selector ?? (x => AsyncObservable.Return(x)).","Ensure config-driven selector factories always return a delegate or an explicit error.","Test the pipeline construction path, not just subscription."],"tags":["asyncrx","argument-null","result-selector","for-operator"],"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"}