dotnet/reactive · error · ArgumentNullException

new ArgumentNullException(nameof(source))

Error message

new ArgumentNullException(nameof(source))

What it means

GroupBy partitions an async observable into IGroupedAsyncObservable streams keyed by keySelector. The library validates the source first and throws ArgumentNullException(nameof(source)) when it is null, consistent with all operator entry points failing fast on null arguments.

Solutions

  1. Verify the upstream operator/factory call — library operators never return null, so find where null entered the chain.
  2. Guard with a null check before calling GroupBy and surface a domain-specific error.
  3. Enable nullable reference types so the null source is caught statically.

Example fix

// before
var groups = maybeSource.GroupBy(x => x.Key);
// after
if (maybeSource == null) throw new InvalidOperationException("source missing");
var groups = maybeSource.GroupBy(x => x.Key);
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new InvalidOperationException("cannot group a null source");

Type guard

static bool IsGroupable<TSource>(IAsyncObservable<TSource>? s) => s is not null;

Try / catch

try { var groups = source.GroupBy(keySelector); } catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* fix upstream source creation */ }

Prevention

When it happens

Trigger: Calling source.GroupBy(keySelector) where source is null — typically the result of a chain or factory that produced null, or a nullable observable field read before assignment.

Common situations: Pipeline builders composing operators conditionally where one branch returns null; configuration-driven source selection missing a mapping; unit tests passing null to operator extension methods directly.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/GroupBy.cs:17

// 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.Reactive.Disposables;
using System.Reactive.Subjects;
using System.Threading.Tasks;

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

            return CreateAsyncObservable<IGroupedAsyncObservable<TKey, TSource>>.From(
                source,
                keySelector,
                static (source, keySelector, observer) => GroupByCore(source, observer, (o, d) => AsyncObserver.GroupBy(o, d, keySelector)));
        }

        public static IAsyncObservable<IGroupedAsyncObservable<TKey, TSource>> GroupBy<TSource, TKey>(this IAsyncObservable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));
            if (comparer == null)
                throw new ArgumentNullException(nameof(comparer));

View on GitHub (pinned to 94b5d5ab91)