dotnet/reactive · error · ArgumentNullException

ArgumentNullException: source

Error message

ArgumentNullException: source

What it means

The GroupBy(source, keySelector, elementSelector) overload requires a non-null source observable. The library validates arguments eagerly at call time and throws ArgumentNullException naming the offending parameter instead of failing later inside the subscription pipeline. This is standard defensive argument checking consistent with System.Reactive conventions.

Solutions

  1. Ensure the observable passed as source is non-null before calling GroupBy
  2. Check upstream factory/repository methods so they return Observable.Empty rather than null
  3. Add a null check or throw a meaningful domain-specific exception at the call site

Example fix

// before
var grouped = source?.GroupBy(x => x.Id, x => x.Data);
// after
if (source == null) throw new InvalidOperationException("source was not initialized");
var grouped = source.GroupBy(x => x.Id, x => x.Data);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) throw new ArgumentNullException(nameof(source));

Type guard

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

Try / catch

try { var g = source.GroupBy(k, e); } catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* recover: rebuild observable */ }

Prevention

When it happens

Trigger: Calling GroupBy and passing null as the source IAsyncObservable<TSource>, e.g. GroupBy(null, x => x.Key, x => x.Value) or when a preceding LINQ chain/variable returned null.

Common situations: Chaining operators on a variable that was never assigned; methods returning null instead of an empty observable; refactoring that removed the source expression; passing the result of a dictionary lookup that missed.

Related errors


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

Appendix: source

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

            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));
            if (capacity < 0)
                throw new ArgumentOutOfRangeException(nameof(capacity));
            if (comparer == null)
                throw new ArgumentNullException(nameof(comparer));

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

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

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

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

View on GitHub (pinned to 94b5d5ab91)