PrismLibrary/Prism · error · ArgumentNullException

Resources.DelegateCommandDelegatesCannotBeNull

Error message

Resources.DelegateCommandDelegatesCannotBeNull

What it means

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.

Solutions

  1. Pass concrete, non-null Action/Func delegates.
  2. Initialize backing fields before constructing the command.
  3. Use the single-argument constructor when no canExecute is needed.

Example fix

// before
SubmitCommand = new DelegateCommand(_submitAction, () => IsValid); // _submitAction null
// after
SubmitCommand = new DelegateCommand(Submit, () => IsValid);
Defensive patterns

Strategy: validation

Validate before calling

if (execute != null && canExecute != null)
    Cmd = new DelegateCommand(execute, canExecute);

Type guard

bool CanBuild(Action exec, Func<bool> canExec) => exec != null && canExec != null;

Try / catch

try { SubmitCommand = new DelegateCommand(Submit, () => IsValid); }
catch (ArgumentNullException ex) { logger.LogError(ex, "DelegateCommand delegate null"); }

Prevention

When it happens

Trigger: new DelegateCommand(null, canExecute) or new DelegateCommand(execute, null); passing delegates from uninitialized fields or reflection-resolved method references that are null.

Common situations: ViewModels wiring commands before their handler methods are assigned; refactors removing methods while the command construction remains; DI factories returning null.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/44227ef1e398787e. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Core/Commands/DelegateCommand.cs:40

        /// </summary>
        /// <param name="executeMethod">The <see cref="Action"/> to invoke when <see cref="ICommand.Execute(object)"/> is called.</param>
        public DelegateCommand(Action executeMethod)
            : this(executeMethod, () => true)
        {

        }

        /// <summary>
        /// Creates a new instance of <see cref="DelegateCommand"/> with the <see cref="Action"/> to invoke on execution
        /// and a <see langword="Func" /> to query for determining if the command can execute.
        /// </summary>
        /// <param name="executeMethod">The <see cref="Action"/> to invoke when <see cref="ICommand.Execute"/> is called.</param>
        /// <param name="canExecuteMethod">The <see cref="Func{TResult}"/> to invoke when <see cref="ICommand.CanExecute"/> is called</param>
        public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod)
            : base()
        {
            if (executeMethod == null || canExecuteMethod == null)
                throw new ArgumentNullException(nameof(executeMethod), Resources.DelegateCommandDelegatesCannotBeNull);

            _executeMethod = executeMethod;
            _canExecuteMethod = canExecuteMethod;
        }

        ///<summary>
        /// Executes the command.
        ///</summary>
        public void Execute()
        {
            try
            {
                _executeMethod();
            }
            catch (Exception ex)
            {
                if (!ExceptionHandler.CanHandle(ex))
                    throw;

View on GitHub (pinned to 358118cd64)