{"record":{"id":"fd3f33f4cb741880","repo":"PrismLibrary/Prism","slug":"resources-delegatecommanddelegatescannotbenull","errorCode":null,"errorMessage":"Resources.DelegateCommandDelegatesCannotBeNull","messagePattern":"Resources\\.DelegateCommandDelegatesCannotBeNull","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Commands/AsyncDelegateCommand.cs","lineNumber":71,"sourceCode":"#if NET6_0_OR_GREATER\n        : this(c => executeMethod().WaitAsync(c), canExecuteMethod)\n#else\n        : this(c => executeMethod(), canExecuteMethod)\n#endif\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{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<CancellationToken, Task> executeMethod, Func<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(CancellationToken? cancellationToken = null)","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Commands/AsyncDelegateCommand.cs#L53-L89","documentation":"AsyncDelegateCommand requires a non-null execute delegate (and, when using the two-argument constructor, a non-null canExecute delegate). The constructor throws ArgumentNullException with Resources.DelegateCommandDelegatesCannotBeNull when either is null, because a command that cannot execute anything is meaningless.","triggerScenarios":"new AsyncDelegateCommand(null) or new AsyncDelegateCommand(someMethodThatReturnsNull, canExecute) — e.g. passing a method group that resolves to null via reflection, or a factory returning null delegates.","commonSituations":"Wiring commands in a ViewModel constructor where the delegate comes from a nullable field or injected service that returned null; refactoring that renamed the target method leaving a null delegate; DI container failing to supply the method.","solutions":["Pass a valid Func<CancellationToken, Task> to the constructor.","Check any factory/lambda producing the delegate for null before constructing the command.","Use the canExecute-aware constructor only when you actually have a non-null canExecute Func<bool>; otherwise use the single-argument overload."],"exampleFix":"// before\npublic MyViewModel()\n{\n    SaveCommand = new AsyncDelegateCommand(_saveFunc); // _saveFunc is null\n}\n// after\npublic MyViewModel()\n{\n    SaveCommand = new AsyncDelegateCommand(async ct => await SaveAsync(ct), () => CanSave);\n}","handlingStrategy":"validation","validationCode":"if (executeMethod == null) throw new InvalidOperationException(\"execute delegate required before constructing AsyncDelegateCommand\");","typeGuard":"bool IsValidCommand(Func<CancellationToken, Task> exec, Func<bool> canExec) => exec != null && canExec != null;","tryCatchPattern":"try { SaveCommand = new AsyncDelegateCommand(handler); }\ncatch (ArgumentNullException ex) { logger.LogError(ex, \"AsyncDelegateCommand delegate was null\"); }","preventionTips":["Construct commands inline with lambda delegates instead of nullable fields.","Initialize all command handlers before ViewModel command construction.","Prefer the single-argument constructor unless canExecute exists."],"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"}