{"record":{"id":"45258fe8c21b79f9","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-selectmany","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'source')","messagePattern":"Value cannot be null\\. \\(Parameter 'source'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/SelectMany.cs","lineNumber":162,"sourceCode":"                throw new ArgumentNullException(nameof(resultSelector));\n\n            return CreateAsyncObservable<TResult>.From(\n                source,\n                (collectionSelector, resultSelector),\n                static async (source, state, observer) =>\n                {\n                    var (sink, inner) = AsyncObserver.SelectMany(observer, state.collectionSelector, state.resultSelector);\n\n                    var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);\n\n                    return StableCompositeAsyncDisposable.Create(subscription, inner);\n                });\n        }\n\n        public static IAsyncObservable<TResult> SelectMany<TSource, TCollection, TResult>(this IAsyncObservable<TSource> source, Func<TSource, int, ValueTask<IAsyncObservable<TCollection>>> collectionSelector, Func<TSource, int, TCollection, int, ValueTask<TResult>> resultSelector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (collectionSelector == null)\n                throw new ArgumentNullException(nameof(collectionSelector));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n\n            return CreateAsyncObservable<TResult>.From(\n                source,\n                (collectionSelector, resultSelector),\n                static async (source, state, observer) =>\n                {\n                    var (sink, inner) = AsyncObserver.SelectMany(observer, state.collectionSelector, state.resultSelector);\n\n                    var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);\n\n                    return StableCompositeAsyncDisposable.Create(subscription, inner);\n                });\n        }\n    }","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/SelectMany.cs#L144-L180","documentation":"SelectMany (the fully async overload with ValueTask-returning collectionSelector and resultSelector) throws ArgumentNullException('source') because the source IAsyncObservable<TSource> was null. Validation is performed synchronously before CreateAsyncObservable builds the pipeline, so the failure is immediate and no resources are leaked. The message 'Value cannot be null. (Parameter 'source')' is the standard .NET ArgumentNullException rendering.","triggerScenarios":"Calling SelectMany<TSource,TCollection,TResult>(source, Func<TSource,int,ValueTask<IAsyncObservable<TCollection>>>, Func<TSource,int,TCollection,int,ValueTask<TResult>>) with source == null; e.g. a null-returning async factory or a field assigned in an async init that has not run.","commonSituations":"Async initialization order bugs (subscription before initialization completes); producers that return null instead of an empty observable on error; missing DI registration.","solutions":["Await the async initialization that assigns the source before composing","Fix the producer to return AsyncObservable.Empty<TSource>() instead of null","Add an explicit null guard with a descriptive exception near the source creation"],"exampleFix":"// before\nvar src = _lazySource; // still null, init not awaited\nvar result = src.SelectMany(cs, rs);\n// after\nawait InitializeAsync();\nvar src = _lazySource ?? AsyncObservable.Empty<TSource>();\nvar result = src.SelectMany(cs, rs);","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\nif (collectionSelector is null) throw new ArgumentNullException(nameof(collectionSelector));\nif (resultSelector is null) throw new ArgumentNullException(nameof(resultSelector));","typeGuard":"static bool IsObservableReady<T>(IAsyncObservable<T>? o) => o is not null;","tryCatchPattern":"try { var result = src.SelectMany(asyncCs, asyncRs); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{ /* await async initialization or substitute an empty observable */ }","preventionTips":["Never consume observables from fields assigned by unfinished async init","Have async factories return empty observables instead of null on failure","Assert non-null sources at composition boundaries","Sequence startup so initialization completes before pipeline construction"],"tags":["csharp","asyncrx","null-check","valuetask"],"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"}