dotnet/reactive · error · ArgumentNullException

ArgumentNullException(nameof(source))

Error message

ArgumentNullException(nameof(source))

What it means

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.

Solutions

  1. Ensure the source sequence is non-null before calling ToLookup.
  2. Substitute AsyncObservable.Empty<TSource>() for a null source when appropriate.
  3. Fix the upstream producer so it never returns null.

Example fix

// before
var lookup = src.ToLookup(x => x.Key, x => x.Val); // src null
// after
var lookup = (src ?? AsyncObservable.Empty<Item>()).ToLookup(x => x.Key, x => x.Val);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) throw new InvalidOperationException("source sequence must be provided before ToLookup");

Type guard

bool IsValidSource<T>(IAsyncObservable<T> s) => s is not null;

Try / catch

try { var lookup = await source.ToLookup(k, v); } catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* handle missing source */ }

Prevention

When it happens

Trigger: Calling ToLookup(null, keySel, valSel) — source is null while the selectors are valid.

Common situations: Grouping pipelines where an upstream operator or factory returned null; nullable-chain expressions evaluated when the receiver was null.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/008eefd9e83bdcb1. Report an issue: GitHub.

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToLookup.cs:16

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information. 

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<ILookup<TKey, TValue>> ToLookup<TSource, TKey, TValue>(this IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));
            if (valueSelector == null)
                throw new ArgumentNullException(nameof(valueSelector));

            return CreateAsyncObservable<ILookup<TKey, TValue>>.From(
                source,
                (keySelector, valueSelector),
                static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.ToLookup(observer, state.keySelector, state.valueSelector)));
        }

        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)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));
            if (valueSelector == null)

View on GitHub (pinned to 94b5d5ab91)