dotnet/reactive · error · ArgumentNullException

nameof(source)

Error message

nameof(source)

What it means

Thrown by ElementAt(source, index) when the source IAsyncObservable is null. ElementAt emits the item at a zero-based position, and the library validates inputs at composition time with ArgumentNullException. The message names source.

Solutions

  1. Pass a non-null IAsyncObservable<TSource> as the first argument
  2. Coalesce with a fallback: source ?? AsyncObservable.Empty<TSource>()
  3. Trace the upstream factory/operator call that produced the null source
  4. Verify argument order: source first, index second

Example fix

// before
var item = AsyncObservable.ElementAt(GetStream(), 2); // GetStream() returned null -> throws
// after
var src = GetStream();
if (src == null) throw new InvalidOperationException("no stream configured");
var item = AsyncObservable.ElementAt(src, 2);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) throw new InvalidOperationException("ElementAt requires a non-null source");
var item = AsyncObservable.ElementAt(source, index);

Type guard

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

Try / catch

try
{
    var item = AsyncObservable.ElementAt(source, index);
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
    // source was null; use a fallback stream or propagate a domain error
    item = AsyncObservable.ElementAt(AsyncObservable.Empty<TSource>(), index);
}

Prevention

When it happens

Trigger: Calling AsyncObservable.ElementAt(null, index) — a null stream from a failed factory, an unassigned member, or swapped arguments where index landed in the source slot.

Common situations: Chaining off a method that returned null instead of an empty observable; dynamic query construction where an optional stream is absent; tests defaulting source to null.

Related errors


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

Appendix: source

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

// 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.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TSource> ElementAt<TSource>(this IAsyncObservable<TSource> source, int index)
        {
            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));

View on GitHub (pinned to 94b5d5ab91)