{"record":{"id":"c005b7928b21c4f1","repo":"PrismLibrary/Prism","slug":"resources-delegatecommanddelegatescannotbenull-c005b7","errorCode":null,"errorMessage":"Resources.DelegateCommandDelegatesCannotBeNull","messagePattern":"Resources\\.DelegateCommandDelegatesCannotBeNull","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Commands/AsyncDelegateCommand{T}.cs","lineNumber":73,"sourceCode":"        : this((p, c) => executeMethod(p).WaitAsync(c), canExecuteMethod)\n#else\n        : this((p, c) => executeMethod(p), canExecuteMethod)\n#endif\n    {\n\n    }\n\n    /// <summary>\n    /// Creates a new instance of <see cref=\"DelegateCommand\"/> with the <see cref=\"Func{Task}\"/> to invoke on execution\n    /// and a <see langword=\"Func\" /> to query for determining if the command can execute.\n    /// </summary>\n    /// <param name=\"executeMethod\">The <see cref=\"Func{T, CancellationToken, Task}\"/> to invoke when <see cref=\"ICommand.Execute\"/> is called.</param>\n    /// <param name=\"canExecuteMethod\">The delegate to invoke when <see cref=\"ICommand.CanExecute\"/> is called</param>\n    public AsyncDelegateCommand(Func<T, CancellationToken, Task> executeMethod, Func<T, bool> canExecuteMethod)\n        : base()\n    {\n        if (executeMethod == null || canExecuteMethod == null)\n            throw new ArgumentNullException(nameof(executeMethod), Resources.DelegateCommandDelegatesCannotBeNull);\n\n        _executeMethod = executeMethod;\n        _canExecuteMethod = canExecuteMethod;\n    }\n\n    /// <summary>\n    /// Gets the current state of the AsyncDelegateCommand\n    /// </summary>\n    public bool IsExecuting\n    {\n        get => _isExecuting;\n        private set => SetProperty(ref _isExecuting, value, OnCanExecuteChanged);\n    }\n\n    ///<summary>\n    /// Executes the command.\n    ///</summary>\n    public async Task Execute(T parameter, CancellationToken? cancellationToken = null)","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Commands/AsyncDelegateCommand{T}.cs#L55-L91","documentation":"The generic AsyncDelegateCommand<T> validates both its execute delegate (Func<T, CancellationToken, Task>) and canExecute delegate (Func<T, bool>) for null and throws ArgumentNullException with Resources.DelegateCommandDelegatesCannotBeNull when either is null. A command must always have an execute path.","triggerScenarios":"new AsyncDelegateCommand<T>(null, canExecute) or new AsyncDelegateCommand<T>(execute, null) using the two-parameter constructor; passing delegates obtained via reflection or a nullable backing field that is null.","commonSituations":"ViewModels constructing typed commands from fields initialized later or from DI-resolved handlers that are null; refactors that changed method signatures leaving nulls.","solutions":["Supply non-null execute and canExecute delegates to the constructor.","Initialize any backing delegate fields before command construction.","Use the single-delegate constructor if only the execute method is available."],"exampleFix":"// before\nDeleteCommand = new AsyncDelegateCommand<Item>(_deleteHandler, _canDelete); // _deleteHandler null\n// after\nDeleteCommand = new AsyncDelegateCommand<Item>(async (item, ct) => await DeleteAsync(item, ct), item => item != null);","handlingStrategy":"validation","validationCode":"if (executeMethod == null || canExecuteMethod == null)\n    throw new InvalidOperationException(\"Both delegates required for AsyncDelegateCommand<T>\");","typeGuard":"bool IsValidCommand<T>(Func<T, CancellationToken, Task> exec, Func<T, bool> canExec) => exec != null && canExec != null;","tryCatchPattern":"try { Cmd = new AsyncDelegateCommand<Item>(handler, canExec); }\ncatch (ArgumentNullException ex) { logger.LogError(ex, \"null delegate for AsyncDelegateCommand<T>\"); }","preventionTips":["Avoid sourcing delegates from reflection without null checks.","Wire typed commands directly to methods, not nullable fields.","Add unit tests constructing all ViewModel commands."],"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"}