dotnet/reactive · error · ArgumentNullException
nameof(source)
Error message
nameof(source)
What it means
ArgumentNullException raised by the parameterless First operator when the source IAsyncObservable is null. First subscribes to the source and forwards the first element, so it validates the source before creating the subscription. FirstAsync overloads delegate to First, so this surfaces when calling First/FirstAsync with a null sequence.
Solutions
- Ensure the observable is non-null before calling First/FirstAsync; throw a meaningful error at the producer
- Use a fallback: (source ?? AsyncObservable.Empty<int>()).First() — note First on an empty sequence errors, so prefer a DefaultIfEmpty stage if emptiness is expected
- Fix the upstream method to return AsyncObservable.Empty instead of null
Example fix
// before
var first = await source.FirstAsync(); // source is null
// after
if (source == null)
throw new InvalidOperationException("Source sequence was not initialized");
var first = await source.FirstAsync(); Defensive patterns
Strategy: validation
Validate before calling
if (source == null)
throw new InvalidOperationException("Source sequence was not initialized");
var first = await source.FirstAsync(); Type guard
bool IsInitialized(IAsyncObservable<int>? s) => s is not null;
Try / catch
try
{
var first = await source.FirstAsync();
}
catch (ArgumentNullException ex) when (ex.ParamName == nameof(source))
{
throw new InvalidOperationException("Source sequence not initialized", ex);
} Prevention
- Return empty observables instead of null from producer methods
- Guard registry/dictionary lookups for the observable before subscribing
- Enable nullable reference types to catch uninitialized observables at compile time
When it happens
Trigger: Calling FirstAsync(source) or source.First() where source is null — e.g. a lookup returned null, a class field was never initialized, or a method chain's earlier stage returned null.
Common situations: Consuming observables resolved from a dictionary/registry by a missing key; repository methods that return null instead of an empty observable; test setups where the subject was not assigned.
Related errors
- nameof(finallyAction)
- nameof(predicate)
- ArgumentNullException
- ArgumentNullException
- Value cannot be null. (Parameter 'onCompletedAsync')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/2cb2a10732e6a807.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/First.cs:18
// 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> FirstAsync<TSource>(this IAsyncObservable<TSource> source) => First(source);
public static IAsyncObservable<TSource> FirstAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate) => First(source, predicate);
public static IAsyncObservable<TSource> FirstAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate) => First(source, predicate);
public static IAsyncObservable<TSource> First<TSource>(this IAsyncObservable<TSource> source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.First(observer)));
}
public static IAsyncObservable<TSource> First<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));
return Create(
source,
predicate,
static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.First(observer, predicate)));
}
public static IAsyncObservable<TSource> First<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)View on GitHub (pinned to 94b5d5ab91)