dotnet/reactive · error · ArgumentNullException
ArgumentNullException(nameof(onError))
Error message
ArgumentNullException(nameof(onError))
What it means
The Do(source, onError) operator throws ArgumentNullException because the onError Action<Exception> side-effect callback is null. The library requires a real callback since Do exists only to invoke it.
Solutions
- Pass a non-null Action<Exception>, even a no-op such as _ => { }
- Guard the handler with a default before composing: handler ?? DefaultHandler
- Fix the producer that was supposed to supply the error handler
Example fix
// before
Action<Exception> onError = config.ErrorHandler; // may be null
var res = src.Do(onError);
// after
var res = src.Do(config.ErrorHandler ?? (ex => { })); Defensive patterns
Strategy: validation
Validate before calling
if (onError is null) onError = _ => { };
var res = source.Do(onError); Type guard
static bool IsValidHandler(Action<Exception> h) => h is not null;
Try / catch
try { var res = source.Do(onError); }
catch (ArgumentNullException ex) when (ex.ParamName == "onError") { /* fall back to no-op handler */ } Prevention
- Default optional delegates to no-ops at declaration, not null
- Use ?? to supply fallback handlers inline
- Check DI/configuration wiring for handler dependencies
- Enable nullable reference types
When it happens
Trigger: Calling source.Do(onError) with a null delegate, e.g. a conditional handler variable that was never assigned or a method parameter forwarded as null.
Common situations: Optional logging/error-handler fields that default to null; DI-injected handlers missing; passing a local that failed to initialize.
Related errors
- ArgumentNullException(nameof(onCompleted))
- Value cannot be null. (Parameter 'onCompletedAsync')
- Value cannot be null. (Parameter 'onNext')
- Value cannot be null. (Parameter 'onError')
- nameof(finallyAction)
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/49c7155eda900bff.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Do.cs:42
public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Action<TSource> onNext)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (onNext == null)
throw new ArgumentNullException(nameof(onNext));
return Create(
source,
onNext,
static (source, onNext, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, onNext)));
}
public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Action<Exception> onError)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (onError == null)
throw new ArgumentNullException(nameof(onError));
return Create(
source,
onError,
static (source, onError, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, onError)));
}
public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Action onCompleted)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (onCompleted == null)
throw new ArgumentNullException(nameof(onCompleted));
return Create(
source,
onCompleted,
static (source, onCompleted, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, onCompleted)));View on GitHub (pinned to 94b5d5ab91)