{"record":{"id":"e767cb7418159875","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-invoke-eventpatternsourcebase","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'invoke')","messagePattern":"Value cannot be null\\. \\(Parameter 'invoke'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/EventPatternSourceBase.cs","lineNumber":109,"sourceCode":"            _invokeHandler = invokeHandler ?? throw new ArgumentNullException(nameof(invokeHandler));\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 <see cref=\"Remove(Delegate)\"/> 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 <c>null</c>.</exception>\n        protected void Add(Delegate handler, Action<TSender?, TEventArgs> invoke)\n        {\n            if (handler == null)\n            {\n                throw new ArgumentNullException(nameof(handler));\n            }\n\n            if (invoke == null)\n            {\n                throw new ArgumentNullException(nameof(invoke));\n            }\n\n            var observer = new Observer(this, handler, invoke);\n            //\n            // [OK] Use of unsafe Subscribe: non-pretentious wrapper of an observable in an event; exceptions can occur during +=.\n            //\n            observer.SetResource(_source.Subscribe(observer));\n        }\n\n        private void Add(Delegate handler, IDisposable disposable)\n        {\n            lock (_subscriptions)\n            {\n                if (!_subscriptions.TryGetValue(handler, out var l))\n                {\n                    _subscriptions[handler] = l = new Stack<IDisposable>();\n                }\n","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/EventPatternSourceBase.cs#L91-L127","documentation":"Add also requires the invocation delegate used to raise the event for each emitted EventPattern; null is rejected because the derived class would have no way to invoke the handler. Fix: pass a non-null invocation delegate alongside the handler.","triggerScenarios":"Calling the protected Add with invoke == null, typically in a derived class whose event-raise action was not initialized or was conditionally created.","commonSituations":"Custom event adapters where the raise action comes from an optional lambda or reflection-built delegate that failed to build.","solutions":["Supply the delegate that invokes the handler on the event target (e.g. h => target.Event += h style raise).","Null-check invoke before calling Add."],"exampleFix":"// before\nAdd(handler, invoke);\n// after\nAdd(handler, invoke ?? (h => { })); // or fix wiring so invoke is real","handlingStrategy":"validation","validationCode":"if (invoke == null) throw new ArgumentNullException(nameof(invoke));","typeGuard":"bool CanAdd<TSender, TArgs>(Action<TSender?, TArgs>? invoke) => invoke is not null;","tryCatchPattern":"try { Add(handler, invoke); } catch (ArgumentNullException ex) when (ex.ParamName == \"invoke\") { /* fix wiring and retry */ }","preventionTips":["Create the raise action together with the handler so neither can be null.","Avoid reflection-built delegates without a null-check on the build result."],"tags":["argument-null","event-handling"],"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"}