{"record":{"id":"d815535e45b8a788","repo":"dotnet/reactive","slug":"throw-new-argumentnullexception-nameof-source-qbservableex","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(source))","messagePattern":"throw new ArgumentNullException\\(nameof\\(source\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/QbservableEx.Homoicon.cs","lineNumber":1023,"sourceCode":"            );\n        }\n#endif\n\n#if !STABLE\n        /// <summary>\n        /// Expands an observable sequence by recursively invoking selector.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence and each of the recursively expanded sources obtained by running the selector function.</typeparam>\n        /// <param name=\"source\">Source sequence with the initial elements.</param>\n        /// <param name=\"selector\">Selector function to invoke for each produced element, resulting in another sequence to which the selector will be invoked recursively again.</param>\n        /// <returns>An observable sequence containing all the elements produced by the recursive expansion.</returns>\n        /// <exception cref=\"ArgumentNullException\">\n        /// <paramref name=\"source\" /> or <paramref name=\"selector\" /> is null.</exception>\n        [Experimental]\n        public static IQbservable<TSource> Expand<TSource>(this IQbservable<TSource> source, Expression<Func<TSource, IObservable<TSource>>> selector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (selector == null)\n                throw new ArgumentNullException(nameof(selector));\n\n            return source.Provider.CreateQuery<TSource>(\n                Expression.Call(\n                    null,\n                    new Func<IQbservable<TSource>, Expression<Func<TSource, IObservable<TSource>>>, IQbservable<TSource>>(Expand<TSource>).Method,\n                    source.Expression,\n                    selector\n                )\n            );\n        }\n#endif\n\n#if !STABLE\n        /// <summary>\n        /// Expands an observable sequence by recursively invoking selector, using the specified scheduler to enumerate the queue of obtained sequences.\n        /// </summary>","sourceCodeStart":1005,"sourceCodeEnd":1041,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/QbservableEx.Homoicon.cs#L1005-L1041","documentation":"QbservableEx.Expand(source, selector) recursively re-expands elements via selector and needs a valid IQbservable source to build the query. A null source throws ArgumentNullException named 'source' immediately at composition time. This is eager validation so bad queries fail at the call site, not on a remote provider.","triggerScenarios":"Calling Qbservable.Expand(null, selector) — commonly when the source query was the result of a null-returning factory, a failed FirstOrDefault-style lookup, or an uninitialized member.","commonSituations":"Chaining Expand after a conditional pipeline where an earlier operator returned null; dictionary/config-driven query construction with a missing key; test doubles returning null instead of an empty qbservable.","solutions":["Pass a non-null IQbservable<TSource>; fall back to Observable.Empty<TSource>() when there is no data","Fix the upstream factory/method that returned a null source","Add a null check with a clear domain message before calling Expand"],"exampleFix":"// before\nsource.Expand(x => Observable.Return(x)); // source is null\n// after\nvar src = source ?? Observable.Empty<TSource>();\nsrc.Expand(x => Observable.Return(x));","handlingStrategy":"validation","validationCode":"if (source == null)\n    throw new InvalidOperationException(\"Expand requires a non-null source; use Observable.Empty<TSource>()\");","typeGuard":"bool HasSource<T>(IQbservable<T> s) => s is not null;","tryCatchPattern":"try\n{\n    var query = source.Expand(x => Observable.Return(x));\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    logger.LogError(ex, \"Expand source was null\");\n}","preventionTips":["Coalesce null sources to Observable.Empty before chaining operators","Fix upstream factories to never return null qbservables","Check for null after lookups that may miss (FirstOrDefault, dictionary TryGet patterns)"],"tags":["argumentnull","rx","expand","qbservable"],"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"}