dotnet/reactive · error · ArgumentNullException

observer

Error message

observer

What it means

Observable.Never's Subscribe validates its observer argument and throws ArgumentNullException when null is passed. Never never emits anything, but it still requires a valid observer to return Disposable.Empty for.

Solutions

  1. Pass a non-null IObserver<TResult> instance to Subscribe.
  2. If you have no observer, use Subscribe(Action<T>) overloads or Observer.Create<T> to build one.
  3. Add a null check before subscribing.

Example fix

// before
Observable.Never<int>().Subscribe(myObserver); // myObserver is null
// after
if (myObserver != null) Observable.Never<int>().Subscribe(myObserver);
Defensive patterns

Strategy: validation

Validate before calling

// C#
if (observer is null) throw new ArgumentNullException(nameof(observer));
var sub = Observable.Never<int>().Subscribe(observer);

Type guard

// C#
static bool IsValidObserver<T>(IObserver<T> o) => o is not null;

Try / catch

try { Observable.Never<int>().Subscribe(observer); }
catch (ArgumentNullException ex) { /* observer was null */ }

Prevention

When it happens

Trigger: Calling Observable.Never<TResult>().Subscribe(null) or passing a null IObserver into the underlying NeverObservable.Subscribe.

Common situations: Programmatic subscription plumbing where the observer variable is uninitialized or the result of a failed factory; reflective/abstracted subscription code paths.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable/Never.cs:29

        /// <summary>
        /// The only instance for a TResult type: this source
        /// is completely stateless and has a constant behavior.
        /// </summary>
        internal static readonly IObservable<TResult> Default = new Never<TResult>();

        /// <summary>
        /// No need for instantiating this more than once per TResult.
        /// </summary>
        private Never()
        {

        }

        public IDisposable Subscribe(IObserver<TResult> observer)
        {
            if (observer == null)
            {
                throw new ArgumentNullException(nameof(observer));
            }

            return Disposable.Empty;
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)