{"record":{"id":"44227ef1e398787e","repo":"PrismLibrary/Prism","slug":"resources-delegatecommanddelegatescannotbenull-44227e","errorCode":null,"errorMessage":"Resources.DelegateCommandDelegatesCannotBeNull","messagePattern":"Resources\\.DelegateCommandDelegatesCannotBeNull","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Commands/DelegateCommand.cs","lineNumber":40,"sourceCode":"        /// </summary>\n        /// <param name=\"executeMethod\">The <see cref=\"Action\"/> to invoke when <see cref=\"ICommand.Execute(object)\"/> is called.</param>\n        public DelegateCommand(Action executeMethod)\n            : this(executeMethod, () => true)\n        {\n\n        }\n\n        /// <summary>\n        /// Creates a new instance of <see cref=\"DelegateCommand\"/> with the <see cref=\"Action\"/> to invoke on execution\n        /// and a <see langword=\"Func\" /> to query for determining if the command can execute.\n        /// </summary>\n        /// <param name=\"executeMethod\">The <see cref=\"Action\"/> to invoke when <see cref=\"ICommand.Execute\"/> is called.</param>\n        /// <param name=\"canExecuteMethod\">The <see cref=\"Func{TResult}\"/> to invoke when <see cref=\"ICommand.CanExecute\"/> is called</param>\n        public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod)\n            : base()\n        {\n            if (executeMethod == null || canExecuteMethod == null)\n                throw new ArgumentNullException(nameof(executeMethod), Resources.DelegateCommandDelegatesCannotBeNull);\n\n            _executeMethod = executeMethod;\n            _canExecuteMethod = canExecuteMethod;\n        }\n\n        ///<summary>\n        /// Executes the command.\n        ///</summary>\n        public void Execute()\n        {\n            try\n            {\n                _executeMethod();\n            }\n            catch (Exception ex)\n            {\n                if (!ExceptionHandler.CanHandle(ex))\n                    throw;","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Commands/DelegateCommand.cs#L22-L58","documentation":"DelegateCommand's two-argument constructor requires non-null execute (Action) and canExecute (Func<bool>) delegates and throws ArgumentNullException with Resources.DelegateCommandDelegatesCannotBeNull otherwise. A command without an execute action cannot fulfill ICommand.Execute.","triggerScenarios":"new DelegateCommand(null, canExecute) or new DelegateCommand(execute, null); passing delegates from uninitialized fields or reflection-resolved method references that are null.","commonSituations":"ViewModels wiring commands before their handler methods are assigned; refactors removing methods while the command construction remains; DI factories returning null.","solutions":["Pass concrete, non-null Action/Func delegates.","Initialize backing fields before constructing the command.","Use the single-argument constructor when no canExecute is needed."],"exampleFix":"// before\nSubmitCommand = new DelegateCommand(_submitAction, () => IsValid); // _submitAction null\n// after\nSubmitCommand = new DelegateCommand(Submit, () => IsValid);","handlingStrategy":"validation","validationCode":"if (execute != null && canExecute != null)\n    Cmd = new DelegateCommand(execute, canExecute);","typeGuard":"bool CanBuild(Action exec, Func<bool> canExec) => exec != null && canExec != null;","tryCatchPattern":"try { SubmitCommand = new DelegateCommand(Submit, () => IsValid); }\ncatch (ArgumentNullException ex) { logger.LogError(ex, \"DelegateCommand delegate null\"); }","preventionTips":["Pass method groups directly instead of nullable delegate fields.","Initialize handlers before command fields.","Favor constructor-ordered initialization in ViewModels."],"tags":["csharp","commands","null-argument"],"backgroundTag":"null-argument","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}