{"record":{"id":"b254badca2964824","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-invoke","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'invoke')","messagePattern":"Value cannot be null\\. \\(Parameter 'invoke'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Internal/EventPatternSourceBaseInternal.cs","lineNumber":52,"sourceCode":"        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\n            //\n            // [OK] Use of unsafe SubscribeAsync: non-pretentious wrapper of an observable in an event; exceptions can occur during +=.","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Internal/EventPatternSourceBaseInternal.cs#L34-L70","documentation":"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.","triggerScenarios":"Calling Add(handler, null) — e.g. the invocation lambda/delegate was never assigned or a conditional expression returned null.","commonSituations":"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.","solutions":["Pass a valid non-null Action<TSender,TEventArgs> invocation delegate.","Assign the invocation delegate before Add is reachable (constructor initialization).","Provide an explicit no-op invocation instead of null if raising is intentionally disabled."],"exampleFix":"// before\nAdd(handler, _invoke); // _invoke assigned later\n// after\nAdd(handler, (sender, args) => _target?.Raise(sender, args));","handlingStrategy":"validation","validationCode":"if (invoke is null) throw new ArgumentNullException(nameof(invoke));\nAdd(handler, invoke);","typeGuard":"static bool HasInvoke(Action<TSender,TEventArgs>? a) => a is not null;","tryCatchPattern":"try { Add(handler, invoke); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"invoke\")\n{ log.LogWarning(\"cannot add handler without an invoke delegate\"); }","preventionTips":["Pass invocation lambdas inline rather than via lazily-assigned fields.","Keep Add/Remove argument orders consistent across derived classes.","Initialize all invocation delegates in the constructor."],"tags":["argument-null","event-pattern","delegate"],"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"}