stride3d/stride · error · ArgumentNullException

ArgumentNullException: viewModel

Error message

ArgumentNullException: viewModel

What it means

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.

Solutions

  1. Construct a valid AssetTemplatesViewModel before creating the window
  2. Check the factory/di result for null and fail earlier with a meaningful message
  3. Show an error dialog instead of opening the window when no view model exists

Example fix

// before
var window = new ItemTemplatesWindow(CreateViewModel());
// after
var vm = CreateViewModel();
if (vm == null) throw new InvalidOperationException("Failed to create asset templates view model");
var window = new ItemTemplatesWindow(vm);
Defensive patterns

Strategy: validation

Validate before calling

if (viewModel == null) { ShowError("Templates view model unavailable"); return; }
var window = new ItemTemplatesWindow(viewModel);

Type guard

static bool HasViewModel([NotNullWhen(true)] AssetTemplatesViewModel? vm) => vm is not null;

Try / catch

try { var w = new ItemTemplatesWindow(vm); w.ShowDialog(); } catch (ArgumentNullException ex) { logger.LogError(ex, "Cannot open templates window without a view model"); }

Prevention

When it happens

Trigger: new ItemTemplatesWindow(viewModel) with viewModel == null, e.g. a failed view-model resolution passed straight into the window.

Common situations: 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).

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/c696958c655ada07. Report an issue: GitHub.

Appendix: source

Thrown at sources/editor/Stride.Core.Assets.Editor/Components/AddAssets/View/ItemTemplatesWindow.xaml.cs:20

// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Stride.Core.Assets.Editor.Components.TemplateDescriptions.ViewModels;
using Stride.Core.Assets.Editor.Services;

namespace Stride.Core.Assets.Editor.Components.AddAssets.View
{
    /// <summary>
    /// Interaction logic for AssetTemplatesWindow.xaml
    /// </summary>
    public partial class ItemTemplatesWindow : IItemTemplateDialog
    {

        public ItemTemplatesWindow(AssetTemplatesViewModel viewModel)
        {
            if (viewModel == null) throw new ArgumentNullException(nameof(viewModel));
            InitializeComponent();
            ViewModel = viewModel;
            Loaded += OnLoaded;
        }

        public AssetTemplatesViewModel ViewModel { get { return (AssetTemplatesViewModel)DataContext; } set { DataContext = value; } }

        public ITemplateDescriptionViewModel SelectedTemplate { get; private set; }

        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseLeftButtonDown(e);
            if (IsMouseOverWindow(e))
            {
                // 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).
                Dispatcher.InvokeAsync(Validate);
            }
        }

View on GitHub (pinned to 96fad776d2)