dotnet/reactive · error · ArgumentNullException

nameof(observer)

Error message

nameof(observer)

What it means

IEnumerable.Subscribe(observer) throws ArgumentNullException when the IObserver<TSource> is null. The observer is the sink for OnNext/OnCompleted from the enumerable; without it the operation is meaningless, so Rx rejects null before subscribing.

Solutions

  1. Pass a valid IObserver<TSource> implementation (or use the Action-based Subscribe overloads).
  2. Check the observer-producing factory/registry for the code path returning null.
  3. Create the observer before starting the subscription rather than in a later initialization step.

Example fix

// before
IObserver<int> obs = _observerHolder.Current; // null until init completes
source.Subscribe(obs); // throws
// after
if (_observerHolder.Current is IObserver<int> obs)
    source.Subscribe(obs);
else
    source.Subscribe(x => Console.WriteLine(x));
Defensive patterns

Strategy: validation

Validate before calling

if (observer is null) observer = new AnonymousObserver<T>(x => Console.WriteLine(x));
var d = source.Subscribe(observer);

Type guard

static bool HasObserver<T>(IObserver<T>? o) => o is not null;

Try / catch

try { var d = source.Subscribe(observer); }
catch (ArgumentNullException ex) when (ex.ParamName == "observer") { log.Error("Null observer passed to Subscribe"); }

Prevention

When it happens

Trigger: Calling source.Subscribe(observer) with observer == null — e.g. a cached observer that failed to initialize, or a delegate-built observer returned null from a factory.

Common situations: Seen when an observer instance is resolved from DI or a registry and comes back null, or when code passes a field that is only assigned in an async initialization path that has not completed.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Conversions.cs:31

        /// <summary>
        /// Subscribes an observer to an enumerable sequence.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Enumerable sequence to subscribe to.</param>
        /// <param name="observer">Observer that will receive notifications from the enumerable sequence.</param>
        /// <returns>Disposable object that can be used to unsubscribe the observer from the enumerable</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="observer"/> is null.</exception>
        public static IDisposable Subscribe<TSource>(this IEnumerable<TSource> source, IObserver<TSource> observer)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (observer == null)
            {
                throw new ArgumentNullException(nameof(observer));
            }

            return s_impl.Subscribe(source, observer);
        }

        /// <summary>
        /// Subscribes an observer to an enumerable sequence, using the specified scheduler to run the enumeration loop.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Enumerable sequence to subscribe to.</param>
        /// <param name="observer">Observer that will receive notifications from the enumerable sequence.</param>
        /// <param name="scheduler">Scheduler to perform the enumeration on.</param>
        /// <returns>Disposable object that can be used to unsubscribe the observer from the enumerable</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="observer"/> or <paramref name="scheduler"/> is null.</exception>
        public static IDisposable Subscribe<TSource>(this IEnumerable<TSource> source, IObserver<TSource> observer, IScheduler scheduler)
        {
            if (source == null)
            {

View on GitHub (pinned to 94b5d5ab91)