dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'observer')

Error message

Value cannot be null. (Parameter 'observer')

What it means

AsyncObserver.DefaultIfEmpty requires an upstream IAsyncObserver<TSource> to wrap. The library eagerly validates arguments and throws ArgumentNullException naming 'observer' when null is passed, because a null observer cannot receive OnNext/OnError/OnCompleted notifications.

Solutions

  1. Pass a non-null IAsyncObserver<TSource> as the first argument to DefaultIfEmpty.
  2. Null-check or use the null-coalescing operator on the observer variable before calling DefaultIfEmpty.
  3. Fix the upstream factory/lookup that produced the null observer instead of propagating it.

Example fix

// before
var obs = GetUpstreamObserver(); // may return null
var withDefault = AsyncObserver.DefaultIfEmpty(obs, 42);
// after
var obs = GetUpstreamObserver() ?? throw new InvalidOperationException("no upstream observer");
var withDefault = AsyncObserver.DefaultIfEmpty(obs, 42);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try
{
    var op = AsyncObserver.DefaultIfEmpty(observer);
}
catch (ArgumentNullException ex) when (ex.ParamName == "observer")
{
    // observer argument was null; fix upstream observer construction
}

Prevention

When it happens

Trigger: Calling AsyncObserver.DefaultIfEmpty<TSource>(null) or DefaultIfEmpty<TSource>(null, defaultValue) — i.e. the first argument is a null IAsyncObserver<TSource>, typically because an upstream observer variable was never assigned or a factory returned null.

Common situations: Composing operator pipelines where an intermediate observer comes from a nullable factory result or dictionary lookup; refactoring that renamed an observer field leaving it uninitialized; passing the result of a conditional expression that falls through to null.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/DefaultIfEmpty.cs:34

        public static IAsyncObservable<TSource> DefaultIfEmpty<TSource>(this IAsyncObservable<TSource> source, TSource defaultValue)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            return Create(
                source,
                defaultValue,
                static (source, defaultValue, observer) => source.SubscribeSafeAsync(AsyncObserver.DefaultIfEmpty(observer, defaultValue)));
        }
    }

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

            return DefaultIfEmpty(observer, default);
        }

        public static IAsyncObserver<TSource> DefaultIfEmpty<TSource>(IAsyncObserver<TSource> observer, TSource defaultValue)
        {
            if (observer == null)
                throw new ArgumentNullException(nameof(observer));

            var hasValue = false;

            return Create<TSource>(
                x =>
                {
                    hasValue = true;
                    return observer.OnNextAsync(x);
                },
                observer.OnErrorAsync,

View on GitHub (pinned to 94b5d5ab91)