dotnet/reactive · error · ArgumentNullException

Value cannot be null.

Error message

Value cannot be null.

What it means

This generated overload of Average (selector returning Int64?) validates both arguments and throws ArgumentNullException when source is null. Generated average operators in AsyncRx.NET follow the same eager-validation pattern: a null source cannot be subscribed to, so the call fails immediately at line 124 of Average.Generated.cs.

Solutions

  1. Ensure the source is non-null before aggregating: use AsyncObservable.Empty<TSource>() as a fallback.
  2. Fix the producer that returned null instead of an empty observable.
  3. Add a null assertion (ArgumentNullException.ThrowIfNull(source) or if-check) where the source is obtained.
  4. If migrating from System.Reactive, note null sources are equally invalid there — locate where the null originated.

Example fix

// before
var avg = (await GetSourceAsync()).Average(x => (long?)x.Value); // GetSourceAsync returned null

// after
var src = await GetSourceAsync() ?? AsyncObservable.Empty<Item>();
var avg = src.Average(x => (long?)x.Value);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null)
    throw new InvalidOperationException("source stream not initialized before Average");

Type guard

static bool HasSource<T>(IAsyncObservable<T>? s) => s is not null;

Try / catch

try
{
    var avg = source.Average(x => (long?)x.Value);
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
    // handle missing stream: log, substitute empty, or rethrow with context
}

Prevention

When it happens

Trigger: Calling Average<TSource>(source, selector) where source is null and selector is the Func<TSource, Int64?> form — e.g. items.AsAsyncObservable() was actually null, or a prior operator returned null.

Common situations: A repository or service method returning null instead of an empty observable; LINQ-style chains built from nullable results of earlier queries; code generated or refactored where an upstream null-check was dropped.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Average.Generated.cs:124

            return CreateAsyncObservable<Double>.From(
                source,
                selector,
                static (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.AverageInt64(observer, selector)));
        }

        public static IAsyncObservable<Double?> Average(this IAsyncObservable<Int64?> source)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            return Create<Int64?, Double?>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.AverageNullableInt64(observer)));
        }

        public static IAsyncObservable<Double?> Average<TSource>(this IAsyncObservable<TSource> source, Func<TSource, Int64?> selector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (selector == null)
                throw new ArgumentNullException(nameof(selector));

            return CreateAsyncObservable<Double?>.From(
                source,
                selector,
                static (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.AverageNullableInt64(observer, selector)));
        }

        public static IAsyncObservable<Double?> Average<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<Int64?>> selector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (selector == null)
                throw new ArgumentNullException(nameof(selector));

            return CreateAsyncObservable<Double?>.From(
                source,

View on GitHub (pinned to 94b5d5ab91)