dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'handler')

Error message

Value cannot be null. (Parameter 'handler')

What it means

EventPatternSourceBase.Add subscribes the handler to the underlying event-pattern source; a null handler delegate cannot be stored or later matched by Remove, so it is rejected. Fix: pass the actual event-handler delegate (the same instance you later pass to Remove).

Solutions

  1. Pass a non-null delegate to Add; ensure custom add/remove accessors supply the actual handler.
  2. Guard: if (handler != null) Add(handler, invoke);

Example fix

// before
Add(handler, invoke);
// after
if (handler != null) { Add(handler, invoke); }
Defensive patterns

Strategy: validation

Validate before calling

if (handler == null) return; // or throw, before calling Add

Type guard

bool CanAdd(Delegate? h) => h is not null;

Try / catch

try { Add(handler, invoke); } catch (ArgumentNullException ex) when (ex.ParamName == "handler") { /* log and skip registration */ }

Prevention

When it happens

Trigger: Calling the protected Add(Delegate handler, Action<TSender?,TEventArgs> invoke) from a derived class with handler == null, e.g. wiring `SomeEvent += null` routed through Add.

Common situations: Derived event-pattern wrappers whose handler is read from a nullable field or a failed delegate lookup before Add is called.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/EventPatternSourceBase.cs:104

        /// <param name="invokeHandler">Delegate used to invoke the event for each element of the sequence.</param>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="invokeHandler"/> is <c>null</c>.</exception>
        protected EventPatternSourceBase(IObservable<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));
        }

        /// <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 <see cref="Remove(Delegate)"/> 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 <c>null</c>.</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 observer = new Observer(this, handler, invoke);
            //
            // [OK] Use of unsafe Subscribe: non-pretentious wrapper of an observable in an event; exceptions can occur during +=.
            //
            observer.SetResource(_source.Subscribe(observer));
        }

        private void Add(Delegate handler, IDisposable disposable)
        {
            lock (_subscriptions)
            {

View on GitHub (pinned to 94b5d5ab91)