{"record":{"id":"56f6d90742128dac","repo":"PrismLibrary/Prism","slug":"resources-delegatecommanddelegatescannotbenull-56f6d9","errorCode":null,"errorMessage":"Resources.DelegateCommandDelegatesCannotBeNull","messagePattern":"Resources\\.DelegateCommandDelegatesCannotBeNull","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Commands/DelegateCommand{T}.cs","lineNumber":60,"sourceCode":"        /// </summary>\n        /// <param name=\"executeMethod\">Delegate to execute when Execute is called on the command. This can be null to just hook up a CanExecute delegate.</param>\n        /// <remarks><see cref=\"CanExecute(T)\"/> will always return true.</remarks>\n        public DelegateCommand(Action<T> executeMethod)\n            : this(executeMethod, (o) => true)\n        {\n        }\n\n        /// <summary>\n        /// Initializes a new instance of <see cref=\"DelegateCommand{T}\"/>.\n        /// </summary>\n        /// <param name=\"executeMethod\">Delegate to execute when Execute is called on the command. This can be null to just hook up a CanExecute delegate.</param>\n        /// <param name=\"canExecuteMethod\">Delegate to execute when CanExecute is called on the command. This can be null.</param>\n        /// <exception cref=\"ArgumentNullException\">When both <paramref name=\"executeMethod\"/> and <paramref name=\"canExecuteMethod\"/> are <see langword=\"null\" />.</exception>\n        public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)\n            : base()\n        {\n            if (executeMethod == null || canExecuteMethod == null)\n                throw new ArgumentNullException(nameof(executeMethod), Resources.DelegateCommandDelegatesCannotBeNull);\n\n            TypeInfo genericTypeInfo = typeof(T).GetTypeInfo();\n\n            // DelegateCommand allows object or Nullable<>.  \n            // note: Nullable<> is a struct so we cannot use a class constraint.\n            if (genericTypeInfo.IsValueType)\n            {\n                if ((!genericTypeInfo.IsGenericType) || (!typeof(Nullable<>).GetTypeInfo().IsAssignableFrom(genericTypeInfo.GetGenericTypeDefinition().GetTypeInfo())))\n                {\n                    throw new InvalidCastException(Resources.DelegateCommandInvalidGenericPayloadType);\n                }\n            }\n\n            _executeMethod = executeMethod;\n            _canExecuteMethod = canExecuteMethod;\n        }\n\n        ///<summary>","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Commands/DelegateCommand{T}.cs#L42-L78","documentation":"The generic DelegateCommand<T>'s two-argument constructor requires non-null execute (Action<T>) and canExecute (Func<T,bool>) delegates and throws ArgumentNullException with Resources.DelegateCommandDelegatesCannotBeNull when either is null.","triggerScenarios":"new DelegateCommand<T>(null, canExecute) or new DelegateCommand<T>(execute, null); delegates sourced from nullable fields, reflection, or DI that are null at construction time.","commonSituations":"Typed ViewModel commands built from handlers assigned later; refactors changing method signatures; DI registration mistakes returning null handlers.","solutions":["Provide non-null execute and canExecute delegates at construction.","Initialize handler fields before command construction.","Use the single-delegate constructor when only execute is available; note T must be object or Nullable<> per the generic checks."],"exampleFix":"// before\nSelectCommand = new DelegateCommand<Item>(_selectHandler); // via (item, null) ctor path handler null\n// after\nSelectCommand = new DelegateCommand<Item>(Select, item => item != null);","handlingStrategy":"validation","validationCode":"if (execute != null && canExecute != null)\n    Cmd = new DelegateCommand<T>(execute, canExecute);","typeGuard":"bool CanBuild<T>(Action<T> exec, Func<T, bool> canExec) => exec != null && canExec != null;","tryCatchPattern":"try { SelectCommand = new DelegateCommand<Item>(Select, CanSelect); }\ncatch (ArgumentNullException ex) { logger.LogError(ex, \"DelegateCommand<T> delegate null\"); }","preventionTips":["Initialize typed handlers before command construction.","Avoid reflection-resolved delegates without null checks.","Add construction tests for every typed command in ViewModels."],"tags":["csharp","commands","null-argument","generics"],"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"}