{"record":{"id":"008eefd9e83bdcb1","repo":"dotnet/reactive","slug":"argumentnullexception-nameof-source-tolookup","errorCode":null,"errorMessage":"ArgumentNullException(nameof(source))","messagePattern":"ArgumentNullException\\(nameof\\(source\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToLookup.cs","lineNumber":16,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<ILookup<TKey, TValue>> ToLookup<TSource, TKey, TValue>(this IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (keySelector == null)\n                throw new ArgumentNullException(nameof(keySelector));\n            if (valueSelector == null)\n                throw new ArgumentNullException(nameof(valueSelector));\n\n            return CreateAsyncObservable<ILookup<TKey, TValue>>.From(\n                source,\n                (keySelector, valueSelector),\n                static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.ToLookup(observer, state.keySelector, state.valueSelector)));\n        }\n\n        public static IAsyncObservable<ILookup<TKey, TValue>> ToLookup<TSource, TKey, TValue>(this IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector, IEqualityComparer<TKey> comparer)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (keySelector == null)\n                throw new ArgumentNullException(nameof(keySelector));\n            if (valueSelector == null)","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToLookup.cs#L1-L34","documentation":"AsyncObservable.ToLookup(source, keySelector, valueSelector) validates the source first. A null IAsyncObservable<TSource> throws ArgumentNullException(Parameter 'source') synchronously at call time, before the lookup operator pipeline is created. This matches the fail-fast convention across all To* operators.","triggerScenarios":"Calling ToLookup(null, keySel, valSel) — source is null while the selectors are valid.","commonSituations":"Grouping pipelines where an upstream operator or factory returned null; nullable-chain expressions evaluated when the receiver was null.","solutions":["Ensure the source sequence is non-null before calling ToLookup.","Substitute AsyncObservable.Empty<TSource>() for a null source when appropriate.","Fix the upstream producer so it never returns null."],"exampleFix":"// before\nvar lookup = src.ToLookup(x => x.Key, x => x.Val); // src null\n// after\nvar lookup = (src ?? AsyncObservable.Empty<Item>()).ToLookup(x => x.Key, x => x.Val);","handlingStrategy":"validation","validationCode":"if (source == null) throw new InvalidOperationException(\"source sequence must be provided before ToLookup\");","typeGuard":"bool IsValidSource<T>(IAsyncObservable<T> s) => s is not null;","tryCatchPattern":"try { var lookup = await source.ToLookup(k, v); } catch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* handle missing source */ }","preventionTips":["Guard nullable upstream results before grouping","Never return null from async observable factories","Compose pipelines from non-null building blocks only"],"tags":["null-argument","asyncrx","lookup"],"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"}