lucasg/Dependencies · error · ArgumentNullException

execute

Error message

execute

What it means

ArgumentNullException thrown by the RelayCommand constructor (param name "execute"). RelayCommand is the standard MVVM ICommand implementation used across Dependencies; it stores an Action<object> to run on Execute and an optional Predicate<object> for CanExecute. Constructing it with a null execute delegate violates the command contract, so the constructor fails fast at creation time rather than producing a NullReferenceException later when Execute is invoked.

Source

Thrown at DependenciesGui/Helpers/RelayCommand.cs:19

using System;
using System.Windows.Input;
using System.Diagnostics;

namespace Dependencies
{
    public class RelayCommand : ICommand
    {
        #region Fields 
        readonly Action<object> _execute;
        readonly Predicate<object> _canExecute;
        #endregion // Fields 

        #region Constructors 
        public RelayCommand(Action<object> execute) : this(execute, null) { }
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute; _canExecute = canExecute;
        }
        #endregion // Constructors 

        #region ICommand Members 
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
        public void Execute(object parameter) { _execute(parameter); }
        #endregion // ICommand Members 
    }

View on GitHub (pinned to 1997a40000)

Solutions

  1. Pass a non-null Action<object> lambda, e.g. `new RelayCommand(_ => Open())`.
  2. If the command is genuinely unused, remove the property/field and its XAML binding instead of passing null.
  3. If the action is optional, guard the caller and skip constructing the command when it is null.
  4. Add a unit test that constructs each command to catch the regression at build time.

Example fix

// before
_OpenNewAppCommand = new RelayCommand(null);

// after
_OpenNewAppCommand = new RelayCommand(_ => OpenNewApp());
Defensive patterns

Strategy: validation

Validate before calling

Action<object> action = ResolveAction();
if (action == null) return null; // or throw a clearer domain error
ICommand cmd = new RelayCommand(action);

Type guard

static bool IsValidCommand(Action<object> execute)
{
    return execute != null;
}

Prevention

When it happens

Trigger: Writing `new RelayCommand(null)` or `new RelayCommand(null, canExecute)` - i.e. the first constructor argument evaluates to null at the moment the command object is created.

Common situations: A refactor removes the action lambda but leaves the `new RelayCommand(...)` call; a method-group/lambda that conditionally resolves to null; a binding/converter yields null that is passed into the constructor; copy-paste of a command definition without the body.


AI-assisted analysis of lucasg/Dependencies@1997a40000 (2026-08-13). Data as JSON: /api/errors/c627e7efdf9cf015. Report an issue: GitHub.