dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'observer')

Error message

Value cannot be null. (Parameter 'observer')

What it means

AsyncObserver.ToArray throws ArgumentNullException because the downstream IAsyncObserver<TSource[]> passed to it is null. This library eagerly validates every public operator argument with ArgumentNullException guards, so calling ToArray with a null observer fails immediately instead of surfacing later inside the aggregation pipeline. It is a programming/usage error, not a runtime data condition.

Solutions

  1. Ensure the observer argument is a non-null IAsyncObserver<TSource[]> before calling ToArray.
  2. Check the code that creates the observer (e.g. CreateObserver or another operator's output) for a path that yields null.
  3. Add an assert/guard at the call site to fail fast with a meaningful message.

Example fix

// before
IAsyncObserver<int[]> observer = BuildObserver(); // may return null
var a = AsyncObserver.ToArray<int>(observer);
// after
var obs = BuildObserver() ?? throw new InvalidOperationException("observer not built");
var a = AsyncObserver.ToArray<int>(obs);
Defensive patterns

Strategy: validation

Validate before calling

if (observer is null) throw new InvalidOperationException("observer must be constructed before ToArray");
var arr = AsyncObserver.ToArray<TSource>(observer);

Type guard

static bool IsValidObserver<T>(IAsyncObserver<T> o) => o is not null;

Try / catch

try { var a = AsyncObserver.ToArray<int>(observer); }
catch (ArgumentNullException ex) when (ex.ParamName == "observer") { /* supply valid observer */ }

Prevention

When it happens

Trigger: Calling AsyncObserver.ToArray<TSource>(null); passing a field or return value that is null because an earlier subscribe/pipe step produced no observer.

Common situations: Refactoring pipelines where an observer variable is not yet assigned; conditional observer construction that returned null on some path; DI containers that failed to resolve the observer dependency.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToArray.cs:25

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TSource[]> ToArray<TSource>(this IAsyncObservable<TSource> source)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            return Create<TSource, TSource[]>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.ToArray(observer)));
        }
    }

    public partial class AsyncObserver
    {
        public static IAsyncObserver<TSource> ToArray<TSource>(IAsyncObserver<TSource[]> observer)
        {
            if (observer == null)
                throw new ArgumentNullException(nameof(observer));

            return Aggregate<TSource, List<TSource>, TSource[]>(observer, new List<TSource>(), (xs, x) => { xs.Add(x); return xs; }, xs => xs.ToArray());
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)