dotnet/reactive · error · ArgumentNullException

nameof(observer)

Error message

nameof(observer)

What it means

This is an ArgumentNullException thrown by AsyncObserver.ElementAt when the observer argument is null. The library validates its arguments up front because the operator factory builds an observer wrapper that would otherwise fail later with a confusing NullReferenceException inside the subscription pipeline. Passing a null IAsyncObserver is always a programming error, so the check throws immediately and synchronously.

Solutions

  1. Ensure a non-null IAsyncObserver<TSource> is passed, e.g. obtained from AsyncObserver.Create or a downstream operator
  2. Check the code path that produced the observer for assignments that can yield null
  3. If the observer may legitimately be absent, gate the call: if (observer != null) AsyncObserver.ElementAt(observer, index)

Example fix

// before
var op = AsyncObserver.ElementAt(observer, 2); // observer is null
// after
if (observer == null) throw new InvalidOperationException("observer not initialized");
var op = AsyncObserver.ElementAt(observer, 2);
Defensive patterns

Strategy: validation

Validate before calling

if (observer is null) throw new ArgumentNullException(nameof(observer));
var op = AsyncObserver.ElementAt(observer, index);

Type guard

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

Try / catch

try { var op = AsyncObserver.ElementAt(observer, index); }
catch (ArgumentNullException ex) when (ex.ParamName == "observer") { /* supply a real observer */ }

Prevention

When it happens

Trigger: Calling AsyncObserver.ElementAt<TSource>(null, index) with a null observer, e.g. when the observer came from an uninitialized field, a failed factory call, or a mis-ordered method chain that returned null.

Common situations: Wiring custom async Rx pipelines by hand where an observer variable is never assigned; refactoring subscription code and accidentally passing null; conditional logic that builds an observer but skips assignment on some path.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAt.cs:30

        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (index < 0)
                throw new ArgumentOutOfRangeException(nameof(index));

            return Create(
                source,
                index,
                static (source, index, observer) => source.SubscribeSafeAsync(AsyncObserver.ElementAt(observer, index)));
        }
    }

    public partial class AsyncObserver
    {
        public static IAsyncObserver<TSource> ElementAt<TSource>(IAsyncObserver<TSource> observer, int index)
        {
            if (observer == null)
                throw new ArgumentNullException(nameof(observer));
            if (index < 0)
                throw new ArgumentOutOfRangeException(nameof(index));

            return Create<TSource>(
                async x =>
                {
                    if (index-- == 0)
                    {
                        await observer.OnNextAsync(x).ConfigureAwait(false);
                        await observer.OnCompletedAsync().ConfigureAwait(false);
                    }
                },
                observer.OnErrorAsync,
                async () =>
                {
                    await observer.OnErrorAsync(new ArgumentOutOfRangeException("The element at the specified index was not found.")).ConfigureAwait(false);
                }
            );

View on GitHub (pinned to 94b5d5ab91)