{"record":{"id":"8ef487cbe33ea66c","repo":"devlooped/moq","slug":"callback-argument-is-null","errorCode":null,"errorMessage":"callback (Argument is null)","messagePattern":"callback \\(Argument is null\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Moq/MethodCall.cs","lineNumber":144,"sourceCode":"\n            this.afterReturnCallback?.Execute(invocation);\n        }\n\n        public void SetCallBaseBehavior()\n        {\n            if (this.Mock.MockedType.IsDelegateType())\n            {\n                throw new NotSupportedException(Resources.CallBaseCannotBeUsedWithDelegateMocks);\n            }\n\n            this.returnOrThrow = ReturnBase.Instance;\n        }\n\n        public void SetCallbackBehavior(Delegate callback)\n        {\n            if (callback == null)\n            {\n                throw new ArgumentNullException(nameof(callback));\n            }\n\n            ref Behavior? behavior = ref (this.returnOrThrow == null) ? ref this.callback\n                                                                     : ref this.afterReturnCallback;\n\n            if (callback is Action callbackWithoutArguments)\n            {\n                behavior = new Callback(_ => callbackWithoutArguments());\n            }\n            else if (callback.GetType() == typeof(Action<IInvocation>))\n            {\n                // NOTE: Do NOT rewrite the above condition as `callback is Action<IInvocation>`,\n                // because this will also yield true if `callback` is a `Action<object>` and thus\n                // break existing uses of `(object arg) => ...` callbacks!\n                behavior = new Callback((Action<IInvocation>)callback);\n            }\n            else\n            {","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/MethodCall.cs#L126-L162","documentation":"SetCallbackBehavior installs the delegate that runs when the mocked member is invoked; Moq requires a non-null callback because a setup whose callback is null would leave the call pipeline with no user behavior to execute and fail later at invocation time. The throw site is a standard ArgumentNullException guard: the faulty input is a null Delegate argument, e.g. mock.Setup(...).Callback(null) or a conditionally-built delegate that turned out to be null.","triggerScenarios":"Calling `.Callback(null)`, or passing a variable/ternary expression that evaluates to null, e.g. `.Callback(someCondition ? action1 : null)`.","commonSituations":"Conditionally selected callbacks where one branch is null; callbacks resolved from configuration or DI that weren't wired up.","solutions":["Pass a non-null lambda or delegate to Callback()","Guard conditional callback selection so a default lambda is always provided","If the callback is optional, skip calling .Callback() entirely instead of passing null"],"exampleFix":"// before\nmock.Setup(m => m.DoWork()).Callback(optionalAction);\n// after\nmock.Setup(m => m.DoWork()).Callback(() => optionalAction?.Invoke());","handlingStrategy":"validation","validationCode":"if (callback == null) throw new InvalidOperationException(\"Callback delegate must not be null before Setup\");","typeGuard":"static bool IsValidCallback(Delegate? d) => d is not null;","tryCatchPattern":"try { setup.Callback(cb); } catch (ArgumentNullException ex) when (ex.ParamName == \"callback\") { /* supply a default lambda */ }","preventionTips":["Never pass nullable delegates directly to Callback()","Use ?? fallback lambdas for conditional callbacks","Skip .Callback() entirely when no action is needed"],"tags":["moq","null-argument","callback","argumentnull"],"backgroundTag":"null-argument","analyzedSha":"89a5be629c752960fe403e95ff583e4ae6f00542","analyzedAt":"2026-09-16T05:31:39.496Z","contentChangedAt":"2026-09-16T05:31:39.496Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}