{"record":{"id":"b7b277e0fcbfc6c6","repo":"PrismLibrary/Prism","slug":"argumentnullexception-for-command","errorCode":null,"errorMessage":"ArgumentNullException for 'command'","messagePattern":"ArgumentNullException for 'command'","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Commands/CompositeCommand.cs","lineNumber":51,"sourceCode":"        /// <param name=\"monitorCommandActivity\">Indicates when the command activity is going to be monitored.</param>\n        public CompositeCommand(bool monitorCommandActivity)\n            : this()\n        {\n            _monitorCommandActivity = monitorCommandActivity;\n        }\n\n        /// <summary>\n        /// Adds a command to the collection and signs up for the <see cref=\"ICommand.CanExecuteChanged\"/> event of it.\n        /// </summary>\n        ///  <remarks>\n        /// If this command is set to monitor command activity, and <paramref name=\"command\"/> \n        /// implements the <see cref=\"IActiveAware\"/> interface, this method will subscribe to its\n        /// <see cref=\"IActiveAware.IsActiveChanged\"/> event.\n        /// </remarks>\n        /// <param name=\"command\">The command to register.</param>\n        public virtual void RegisterCommand(ICommand command)\n        {\n            if (command == null) throw new ArgumentNullException(nameof(command));\n            if (command == this)\n            {\n                throw new ArgumentException(Resources.CannotRegisterCompositeCommandInItself);\n            }\n\n            lock (_registeredCommands)\n            {\n                if (_registeredCommands.Contains(command))\n                {\n                    throw new InvalidOperationException(Resources.CannotRegisterSameCommandTwice);\n                }\n                _registeredCommands.Add(command);\n            }\n\n            command.CanExecuteChanged += _onRegisteredCommandCanExecuteChangedHandler;\n            OnCanExecuteChanged();\n\n            if (_monitorCommandActivity)","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Commands/CompositeCommand.cs#L33-L69","documentation":"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.","triggerScenarios":"compositeCommand.RegisterCommand(null), typically after a command field/property is null at wiring time.","commonSituations":"Building a toolbar composite command from ViewModel properties that are not yet initialized; DI not having created a child command before registration.","solutions":["Ensure child commands are constructed before calling RegisterCommand.","Null-check child commands at the call site or assert during initialization.","Register commands in a post-initialization hook (e.g. OnNavigatedTo or constructor after all fields are set)."],"exampleFix":"// before\ncomposite.RegisterCommand(_saveCommand); // null\n// after\nif (_saveCommand != null)\n    composite.RegisterCommand(_saveCommand);","handlingStrategy":"validation","validationCode":"if (childCommand != null)\n    composite.RegisterCommand(childCommand);","typeGuard":null,"tryCatchPattern":"try { composite.RegisterCommand(child); }\ncatch (ArgumentNullException ex) { logger.LogError(ex, \"child command was null at registration\"); }","preventionTips":["Create child commands before composite registration.","Register in constructors, not deferred lifecycle hooks where fields may be null.","Use constructor injection to guarantee child command availability."],"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"}