dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'handler')
Error message
Value cannot be null. (Parameter 'handler')
What it means
The protected Add(Delegate handler, Action<TSender,TEventArgs> invoke) method registers an event handler and starts a subscription; both arguments are null-checked first. A null handler is rejected with ArgumentNullException because it is used as the dictionary key for later Remove.
Solutions
- Pass a non-null Delegate as handler.
- Add a null check in the public wrapper before calling protected Add so the caller gets a clearer error.
- If the handler may be absent, skip the Add call instead of forwarding null.
Example fix
// before
public void AddHandler(EventHandler<EventArgs> h) => Add(h, (s, e) => h?.Invoke(s, e));
// after
public void AddHandler(EventHandler<EventArgs> h)
{
if (h == null) throw new ArgumentNullException(nameof(h));
Add(h, (s, e) => h(s, e));
} Defensive patterns
Strategy: validation
Validate before calling
if (handler is null) throw new ArgumentNullException(nameof(handler)); Add(handler, invoke);
Type guard
static bool HasHandler(Delegate? d) => d is not null;
Try / catch
try { Add(handler, invoke); }
catch (ArgumentNullException ex) when (ex.ParamName == "handler")
{ log.LogWarning("cannot add a null event handler"); } Prevention
- Null-check handler in public wrapper APIs before reaching protected Add.
- Do not forward optional handlers; skip the call instead.
- Enable nullable annotations on handler parameters.
When it happens
Trigger: Calling Add(null, invoke) in a derived event-pattern class, e.g. forwarding an event-handler argument that was null from the public API surface.
Common situations: A public AddHandler(Handler h) forwarding h directly without checking; callers passing a lambda they expected to be wrapped; marshalled/UI event handlers that failed to resolve.
Related errors
- Value cannot be null. (Parameter 'source')
- Value cannot be null. (Parameter 'invokeHandler')
- Value cannot be null. (Parameter 'invoke')
- Value cannot be null. (Parameter 'source')
- Value cannot be null. (Parameter 'invokeHandler')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/1de26fe187042bae.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Internal/EventPatternSourceBaseInternal.cs:50
/// <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 null.</exception>
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;
}
});
View on GitHub (pinned to 94b5d5ab91)