dotnet/reactive · error · ArgumentNullException

source

Error message

source

What it means

The Subscribe<T>(this IObservable<T>) extension throws ArgumentNullException when the source sequence is null. Rx validates the receiver before constructing the AnonymousObserver that ignores values, rethrows errors, and no-ops on completion. Thrown synchronously at subscription time, before any observer callbacks.

Solutions

  1. Ensure the source is initialized before subscribing; fix whatever returns null.
  2. Use source?.Subscribe() with null-conditional access if subscribing a null source should be a no-op.
  3. Use Observable.Empty<T>() as a fallback instead of null to keep the chain type-safe.

Example fix

// before
source.Subscribe(); // source is null
// after
(source ?? Observable.Empty<int>()).Subscribe();
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) return System.Reactive.Disposables.Disposable.Empty;
source.Subscribe();

Type guard

bool IsSubscribable<T>(IObservable<T> source) => source != null;

Try / catch

try { source.Subscribe(); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* log: observable source was null */ }

Prevention

When it happens

Trigger: Calling source.Subscribe() where the IObservable<T> variable is null — e.g. a factory/property returned null, a dictionary lookup missed, or a chain like GetObservable()?.Subscribe() resolved to null via !.

Common situations: DI/container returning null for a registered-or-not service; optional observables that are null when a feature is disabled; test doubles not stubbed.

Related errors


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

Appendix: source

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

    /// Provides a set of static methods for subscribing delegates to observables.
    /// </summary>
    public static class ObservableExtensions
    {
        #region Subscribe delegate-based overloads

        /// <summary>
        /// Subscribes to the observable sequence without specifying any handlers.
        /// This method can be used to evaluate the observable sequence for its side-effects only.
        /// </summary>
        /// <typeparam name="T">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Observable sequence to subscribe to.</param>
        /// <returns><see cref="IDisposable"/> object used to unsubscribe from the observable sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception>
        public static IDisposable Subscribe<T>(this IObservable<T> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            //
            // [OK] Use of unsafe Subscribe: non-pretentious constructor for an observer; this overload is not to be used internally.
            //
            return source.Subscribe/*Unsafe*/(new AnonymousObserver<T>(Stubs<T>.Ignore, Stubs.Throw, Stubs.Nop));
        }

        /// <summary>
        /// Subscribes an element handler to an observable sequence.
        /// </summary>
        /// <typeparam name="T">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Observable sequence to subscribe to.</param>
        /// <param name="onNext">Action to invoke for each element in the observable sequence.</param>
        /// <returns><see cref="IDisposable"/> object used to unsubscribe from the observable sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> is <c>null</c>.</exception>
        public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext)
        {

View on GitHub (pinned to 94b5d5ab91)