dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'onError')

Error message

Value cannot be null. (Parameter 'onError')

What it means

The Do(source, onNext, onError) overload requires a non-null onError action and throws ArgumentNullException with parameter name 'onError' when it is null. onError is invoked on exceptional termination of the sequence, and the library rejects a null eagerly at the call site.

Solutions

  1. Pass a no-op `_ => { }` for onError if error notification is not needed
  2. Use the Do(source, onNext) overload which supplies an internal error handler
  3. Default the handler: `onError ??= _ => { };` before calling
  4. Register/assign the error handler in the DI container or configuration path that produces it

Example fix

// before
var result = source.Do(x => Log(x), null);
// after
var result = source.Do(x => Log(x), _ => { });
Defensive patterns

Strategy: validation

Validate before calling

if (onError is null) onError = static _ => { };

Type guard

static Action<Exception> NonNullErrorHandler(Action<Exception>? h) => h ?? (static _ => { });

Try / catch

try { var result = source.Do(x => Log(x), onError!); ... }
catch (ArgumentNullException ex) when (ex.ParamName == "onError") { Log("onError callback missing; using no-op"); }

Prevention

When it happens

Trigger: Calling `source.Do(x => Log(x), null)` or `source.Do(x => Log(x), null, () => Done())`; a conditionally assigned error handler that ends up null.

Common situations: Optional error logging configured off in some environments leaving the handler null; refactoring where the onError lambda was removed but the call retained its slot; DI-resolved handlers missing a registration.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/Do.cs:63

            return DoCore(source, onNext, _ => { }, onCompleted);
        }

        /// <summary>
        /// Lazily invokes an action for each value in the sequence, and executes an action upon exceptional termination.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="onNext">Action to invoke for each element.</param>
        /// <param name="onError">Action to invoke on exceptional termination of the sequence.</param>
        /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
        public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> 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));

            return DoCore(source, onNext, onError, () => { });
        }

        /// <summary>
        /// Lazily invokes an action for each value in the sequence, and executes an action upon successful or exceptional
        /// termination.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="onNext">Action to invoke for each element.</param>
        /// <param name="onError">Action to invoke on exceptional termination of the sequence.</param>
        /// <param name="onCompleted">Action to invoke on successful termination of the sequence.</param>
        /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
        public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

View on GitHub (pinned to 94b5d5ab91)