dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'invoke')

Error message

Value cannot be null. (Parameter 'invoke')

What it means

Add(Delegate handler, Action<TSender,TEventArgs> invoke) also requires a non-null invoke delegate, which the derived class uses to raise the event per element. A null invoke is rejected with ArgumentNullException at call time.

Solutions

  1. Pass a valid non-null Action<TSender,TEventArgs> invocation delegate.
  2. Assign the invocation delegate before Add is reachable (constructor initialization).
  3. Provide an explicit no-op invocation instead of null if raising is intentionally disabled.

Example fix

// before
Add(handler, _invoke); // _invoke assigned later
// after
Add(handler, (sender, args) => _target?.Raise(sender, args));
Defensive patterns

Strategy: validation

Validate before calling

if (invoke is null) throw new ArgumentNullException(nameof(invoke));
Add(handler, invoke);

Type guard

static bool HasInvoke(Action<TSender,TEventArgs>? a) => a is not null;

Try / catch

try { Add(handler, invoke); }
catch (ArgumentNullException ex) when (ex.ParamName == "invoke")
{ log.LogWarning("cannot add handler without an invoke delegate"); }

Prevention

When it happens

Trigger: Calling Add(handler, null) — e.g. the invocation lambda/delegate was never assigned or a conditional expression returned null.

Common situations: Derived classes passing a cached delegate field that is still null; refactors removing the invocation method without updating the Add call; overloads of Add where the wrong one is invoked with null.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Internal/EventPatternSourceBaseInternal.cs:52

        protected EventPatternSourceBaseInternal(IAsyncObservable<EventPattern<TSender, TEventArgs>> source, Action<Action<TSender, TEventArgs>, /*object,*/ EventPattern<TSender, TEventArgs>> invokeHandler)
        {
            _source = source ?? throw new ArgumentNullException(nameof(source));
            _invokeHandler = invokeHandler ?? throw new ArgumentNullException(nameof(invokeHandler));
            _subscriptions = new Dictionary<Delegate, Stack<IAsyncDisposable>>();
        }

        /// <summary>
        /// Adds the specified event handler, causing a subscription to the underlying source.
        /// </summary>
        /// <param name="handler">Event handler to add. The same delegate should be passed to the Remove operation in order to remove the event handler.</param>
        /// <param name="invoke">Invocation delegate to raise the event in the derived class.</param>
        /// <exception cref="ArgumentNullException"><paramref name="handler"/> or <paramref name="invoke"/> is null.</exception>
        protected void Add(Delegate handler, Action<TSender, TEventArgs> invoke)
        {
            if (handler == null)
                throw new ArgumentNullException(nameof(handler));
            if (invoke == null)
                throw new ArgumentNullException(nameof(invoke));

            var gate = new object();
            var isAdded = false;
            var isDone = false;

            var remove = new Action(() =>
            {
                lock (gate)
                {
                    if (isAdded)
                        Remove(handler);
                    else
                        isDone = true;
                }
            });

            //
            // [OK] Use of unsafe SubscribeAsync: non-pretentious wrapper of an observable in an event; exceptions can occur during +=.

View on GitHub (pinned to 94b5d5ab91)