dotnet/reactive · error · ArgumentNullException

nameof(source)

Error message

nameof(source)

What it means

This is an ArgumentNullException thrown by AsyncObservable.ElementAtOrDefault when the source observable is null. The extension method validates its receiver up front so the failure surfaces at the call site instead of deep inside subscription plumbing. A null source is always a caller bug.

Solutions

  1. Ensure the IAsyncObservable<TSource> is non-null before the call
  2. Fix the producer/factory that returned null
  3. Guard optional sources: use source?.ElementAtOrDefault(index) or an explicit null check

Example fix

// before
var result = source.ElementAtOrDefault(3); // source null
// after
var result = source?.ElementAtOrDefault(3);
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new ArgumentNullException(nameof(source));
var result = source.ElementAtOrDefault(index);

Type guard

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

Try / catch

try { var result = source.ElementAtOrDefault(index); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* fall back to an empty observable */ }

Prevention

When it happens

Trigger: Invoking source.ElementAtOrDefault(index) or AsyncObservable.ElementAtOrDefault(null, index) where the observable came from an uninitialized variable, a factory that returned null, or a chain that short-circuited to null.

Common situations: Building observables from config/conditional factories that can return null; forgetting a null result from a lookup of registered streams; refactoring where a source field was no longer initialized.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAtOrDefault.cs:12

// 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. 

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TSource> ElementAtOrDefault<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.ElementAtOrDefault(observer, index)));
        }
    }

    public partial class AsyncObserver
    {
        public static IAsyncObserver<TSource> ElementAtOrDefault<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)