dotnet/reactive · error · ArgumentNullException

observer

Error message

observer

What it means

ListObservable's Subscribe(IObserver<object>) replays recorded notifications via an internal subject and throws ArgumentNullException when 'observer' is null, because there is no meaningful subscription target.

Solutions

  1. Pass a valid IObserver<object> (e.g. AnonymousObserver) instead of null
  2. Null-check the observer before subscribing
  3. Fix the code path that produced a null observer

Example fix

// before
listObservable.Subscribe(maybeObserver); // may be null
// after
var observer = maybeObserver ?? new AnonymousObserver<object>(x => Console.WriteLine(x));
listObservable.Subscribe(observer);
Defensive patterns

Strategy: validation

Validate before calling

if (observer == null) throw new ArgumentNullException(nameof(observer));
listObservable.Subscribe(observer);

Type guard

static bool IsValidObserver(IObserver<object> o) => o != null;

Try / catch

try { listObservable.Subscribe(observer); } catch (ArgumentNullException) { /* null observer */ }

Prevention

When it happens

Trigger: Calling listObservable.Subscribe(null), typically when the observer comes from a failed lookup, an uninitialized field, or a factory returning null.

Common situations: Test code building observers conditionally; a refactoring replaced a real observer with a null-returning helper; event-wiring code where the handler was never attached.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/ListObservable.cs:204

        public IEnumerator<T> GetEnumerator()
        {
            Wait();
            return _results.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

        /// <summary>
        /// Subscribes an observer to the ListObservable which will be notified upon completion.
        /// </summary>
        /// <param name="observer">The observer to send completion or error messages to.</param>
        /// <returns>The disposable resource that can be used to unsubscribe.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="observer"/> is <c>null</c>.</exception>
        public IDisposable Subscribe(IObserver<object> observer)
        {
            if (observer == null)
            {
                throw new ArgumentNullException(nameof(observer));
            }

            return StableCompositeDisposable.Create(_subscription, _subject.Subscribe(observer));
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)