{"record":{"id":"c696958c655ada07","repo":"stride3d/stride","slug":"argumentnullexception-viewmodel","errorCode":null,"errorMessage":"ArgumentNullException: viewModel","messagePattern":"ArgumentNullException: viewModel","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"sources/editor/Stride.Core.Assets.Editor/Components/AddAssets/View/ItemTemplatesWindow.xaml.cs","lineNumber":20,"sourceCode":"// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.\nusing System;\nusing System.Windows;\nusing System.Windows.Controls;\nusing System.Windows.Input;\nusing Stride.Core.Assets.Editor.Components.TemplateDescriptions.ViewModels;\nusing Stride.Core.Assets.Editor.Services;\n\nnamespace Stride.Core.Assets.Editor.Components.AddAssets.View\n{\n    /// <summary>\n    /// Interaction logic for AssetTemplatesWindow.xaml\n    /// </summary>\n    public partial class ItemTemplatesWindow : IItemTemplateDialog\n    {\n\n        public ItemTemplatesWindow(AssetTemplatesViewModel viewModel)\n        {\n            if (viewModel == null) throw new ArgumentNullException(nameof(viewModel));\n            InitializeComponent();\n            ViewModel = viewModel;\n            Loaded += OnLoaded;\n        }\n\n        public AssetTemplatesViewModel ViewModel { get { return (AssetTemplatesViewModel)DataContext; } set { DataContext = value; } }\n\n        public ITemplateDescriptionViewModel SelectedTemplate { get; private set; }\n\n        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)\n        {\n            base.OnPreviewMouseLeftButtonDown(e);\n            if (IsMouseOverWindow(e))\n            {\n                // Defer the validation so the list box has time to update the selected template (we are fired before it because it is the preview event).\n                Dispatcher.InvokeAsync(Validate);\n            }\n        }","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/editor/Stride.Core.Assets.Editor/Components/AddAssets/View/ItemTemplatesWindow.xaml.cs#L2-L38","documentation":"Argument-null validation in the ItemTemplatesWindow constructor: the viewModel parameter (the AssetTemplatesViewModel supplying the template list to display) is null. The window cannot render the asset template selection dialog without a view model, so the constructor rejects the call rather than creating a non-functional dialog.","triggerScenarios":"new ItemTemplatesWindow(viewModel) with viewModel == null, e.g. a failed view-model resolution passed straight into the window.","commonSituations":"Wiring the Add Assets dialog in the editor when the view model factory returned null (dependency resolution failure or null templates earlier in the pipeline).","solutions":["Construct a valid AssetTemplatesViewModel before creating the window","Check the factory/di result for null and fail earlier with a meaningful message","Show an error dialog instead of opening the window when no view model exists"],"exampleFix":"// before\nvar window = new ItemTemplatesWindow(CreateViewModel());\n// after\nvar vm = CreateViewModel();\nif (vm == null) throw new InvalidOperationException(\"Failed to create asset templates view model\");\nvar window = new ItemTemplatesWindow(vm);","handlingStrategy":"validation","validationCode":"if (viewModel == null) { ShowError(\"Templates view model unavailable\"); return; }\nvar window = new ItemTemplatesWindow(viewModel);","typeGuard":"static bool HasViewModel([NotNullWhen(true)] AssetTemplatesViewModel? vm) => vm is not null;","tryCatchPattern":"try { var w = new ItemTemplatesWindow(vm); w.ShowDialog(); } catch (ArgumentNullException ex) { logger.LogError(ex, \"Cannot open templates window without a view model\"); }","preventionTips":["Resolve the view model before constructing its window","Fail fast with a user-visible message instead of passing null down","Keep view-model construction and window construction adjacent in code"],"tags":["null-argument","wpf","dialog"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}