PrismLibrary/Prism · error · ArgumentNullException

ArgumentNullException for 'command'

Error message

ArgumentNullException for 'command'

What it means

Raised in CompositeCommand.RegisterCommand when the ICommand passed to be registered is null. A composite command can only subscribe to CanExecuteChanged and track activity of real command instances, so a null command argument is rejected before it is added to the registered-commands collection.

Solutions

  1. Ensure child commands are constructed before calling RegisterCommand.
  2. Null-check child commands at the call site or assert during initialization.
  3. Register commands in a post-initialization hook (e.g. OnNavigatedTo or constructor after all fields are set).

Example fix

// before
composite.RegisterCommand(_saveCommand); // null
// after
if (_saveCommand != null)
    composite.RegisterCommand(_saveCommand);
Defensive patterns

Strategy: validation

Validate before calling

if (childCommand != null)
    composite.RegisterCommand(childCommand);

Try / catch

try { composite.RegisterCommand(child); }
catch (ArgumentNullException ex) { logger.LogError(ex, "child command was null at registration"); }

Prevention

When it happens

Trigger: compositeCommand.RegisterCommand(null), typically after a command field/property is null at wiring time.

Common situations: Building a toolbar composite command from ViewModel properties that are not yet initialized; DI not having created a child command before registration.

Related errors


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

Appendix: source

Thrown at src/Prism.Core/Commands/CompositeCommand.cs:51

        /// <param name="monitorCommandActivity">Indicates when the command activity is going to be monitored.</param>
        public CompositeCommand(bool monitorCommandActivity)
            : this()
        {
            _monitorCommandActivity = monitorCommandActivity;
        }

        /// <summary>
        /// Adds a command to the collection and signs up for the <see cref="ICommand.CanExecuteChanged"/> event of it.
        /// </summary>
        ///  <remarks>
        /// If this command is set to monitor command activity, and <paramref name="command"/> 
        /// implements the <see cref="IActiveAware"/> interface, this method will subscribe to its
        /// <see cref="IActiveAware.IsActiveChanged"/> event.
        /// </remarks>
        /// <param name="command">The command to register.</param>
        public virtual void RegisterCommand(ICommand command)
        {
            if (command == null) throw new ArgumentNullException(nameof(command));
            if (command == this)
            {
                throw new ArgumentException(Resources.CannotRegisterCompositeCommandInItself);
            }

            lock (_registeredCommands)
            {
                if (_registeredCommands.Contains(command))
                {
                    throw new InvalidOperationException(Resources.CannotRegisterSameCommandTwice);
                }
                _registeredCommands.Add(command);
            }

            command.CanExecuteChanged += _onRegisteredCommandCanExecuteChangedHandler;
            OnCanExecuteChanged();

            if (_monitorCommandActivity)

View on GitHub (pinned to 358118cd64)