dotnet/reactive · error · ArgumentNullException

onError

Error message

onError

What it means

The Subscribe(source, onNext, onError) overload throws ArgumentNullException with ParamName "onError" when the onError Action<Exception> callback is null. Rx validates it eagerly because AnonymousObserver requires a concrete error handler; a null error handler would make notification routing impossible.

Solutions

  1. Pass a non-null onError delegate, e.g. ex => throw ex or a logging action.
  2. If you do not want to handle errors, use the onCompleted-only overload (source.Subscribe(onNext, onCompleted)), which supplies Stubs.Throw internally.
  3. Verify the handler field is populated before subscribing.

Example fix

// before
source.Subscribe(x => Handle(x), null); // ArgumentNullException: onError

// after
source.Subscribe(x => Handle(x), ex => Console.WriteLine(ex));
Defensive patterns

Strategy: validation

Validate before calling

if (onError is null) onError = ex => Console.Error.WriteLine(ex); // or use Subscribe(onNext, onCompleted) overload

Type guard

static bool HasErrorHandler<T>(Action<T> onNext, Action<Exception> onError) => onNext is not null && onError is not null;

Try / catch

try { disposable = source.Subscribe(onNext, onError); }
catch (ArgumentNullException ex) when (ex.ParamName == "onError") { /* install default logging handler */ }

Prevention

When it happens

Trigger: Calling source.Subscribe(onNext, null) or passing an error-handler variable that was never assigned or was set to null by configuration.

Common situations: Logging hooks configured to null to 'disable' error logging; test scaffolding that only supplies onNext; refactor that moved the error handler but left the call site unchanged.

Related errors


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

Appendix: source

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

        /// <param name="onNext">Action to invoke for each element in the observable sequence.</param>
        /// <param name="onError">Action to invoke upon exceptional termination of 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"/> or <paramref name="onError"/> is <c>null</c>.</exception>
        public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action<Exception> onError)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

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

            //
            // [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>(onNext, onError, Stubs.Nop));
        }

        /// <summary>
        /// Subscribes an element handler and a completion 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>
        /// <param name="onCompleted">Action to invoke upon graceful termination of 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"/> or <paramref name="onCompleted"/> is <c>null</c>.</exception>
        public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action onCompleted)

View on GitHub (pinned to 94b5d5ab91)