{"record":{"id":"1de26fe187042bae","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-handler","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'handler')","messagePattern":"Value cannot be null\\. \\(Parameter 'handler'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Internal/EventPatternSourceBaseInternal.cs","lineNumber":50,"sourceCode":"        /// <param name=\"invokeHandler\">Delegate used to invoke the event for each element of the sequence.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"invokeHandler\"/> is null.</exception>\n        protected EventPatternSourceBaseInternal(IAsyncObservable<EventPattern<TSender, TEventArgs>> source, Action<Action<TSender, TEventArgs>, /*object,*/ EventPattern<TSender, TEventArgs>> invokeHandler)\n        {\n            _source = source ?? throw new ArgumentNullException(nameof(source));\n            _invokeHandler = invokeHandler ?? throw new ArgumentNullException(nameof(invokeHandler));\n            _subscriptions = new Dictionary<Delegate, Stack<IAsyncDisposable>>();\n        }\n\n        /// <summary>\n        /// Adds the specified event handler, causing a subscription to the underlying source.\n        /// </summary>\n        /// <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>\n        /// <param name=\"invoke\">Invocation delegate to raise the event in the derived class.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"handler\"/> or <paramref name=\"invoke\"/> is null.</exception>\n        protected void Add(Delegate handler, Action<TSender, TEventArgs> invoke)\n        {\n            if (handler == null)\n                throw new ArgumentNullException(nameof(handler));\n            if (invoke == null)\n                throw new ArgumentNullException(nameof(invoke));\n\n            var gate = new object();\n            var isAdded = false;\n            var isDone = false;\n\n            var remove = new Action(() =>\n            {\n                lock (gate)\n                {\n                    if (isAdded)\n                        Remove(handler);\n                    else\n                        isDone = true;\n                }\n            });\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Internal/EventPatternSourceBaseInternal.cs#L32-L68","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublic void AddHandler(EventHandler<EventArgs> h) => Add(h, (s, e) => h?.Invoke(s, e));\n// after\npublic void AddHandler(EventHandler<EventArgs> h)\n{\n    if (h == null) throw new ArgumentNullException(nameof(h));\n    Add(h, (s, e) => h(s, e));\n}","handlingStrategy":"validation","validationCode":"if (handler is null) throw new ArgumentNullException(nameof(handler));\nAdd(handler, invoke);","typeGuard":"static bool HasHandler(Delegate? d) => d is not null;","tryCatchPattern":"try { Add(handler, invoke); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"handler\")\n{ log.LogWarning(\"cannot add a null event handler\"); }","preventionTips":["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."],"tags":["argument-null","event-pattern","event-handler"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}