stride3d/stride · error · ArgumentNullException

Value cannot be null. (Parameter 'image')

Error message

Value cannot be null. (Parameter 'image')

What it means

ImageExtensions.SetSource(this Image, Uri) assigns image.Source from a file Uri and validates both arguments, throwing ArgumentNullException when the Image control itself is null. This typically happens when the extension method is invoked on a null receiver or with a null first argument.

Solutions

  1. Null-check the Image before calling SetSource.
  2. Resolve the Image after the page is loaded (Loaded event or OnApplyTemplate).
  3. Fix the lookup returning null (verify x:Name and control presence).
  4. Prefer setting the Source property directly with a null-safe wrapper if the image may be absent.

Example fix

// before
var img = FindName("Logo") as Image;
img.SetSource(new Uri("pack://application:,,,/logo.png")); // NRE-free but throws ArgumentNullException inside
// after
var img = FindName("Logo") as Image;
if (img != null)
    img.SetSource(new Uri("pack://application:,,,/logo.png"));
Defensive patterns

Strategy: validation

Validate before calling

var img = FindName("Logo") as Image;
if (img == null) return; // or log
img.SetSource(new Uri("pack://application:,,,/logo.png"));

Type guard

static bool CanSetSource(Image image, Uri uri) => image != null && uri != null;

Try / catch

try { image.SetSource(uri); }
catch (ArgumentNullException ex) when (ex.ParamName == "image") { logger.Warn("Image control not found; skipping source assignment"); }

Prevention

When it happens

Trigger: Calling image.SetSource(uri) where the Image instance is null (e.g. FindName/FindResource lookup failed) — a null this on an extension method call does not throw NullReferenceException but flows into the guard.

Common situations: XAML control resolved by name at a point where the visual tree is not yet built; Image removed from the tree before the code runs; a view-model binding produced a null Image reference in generated/code-behind code.

Related errors


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

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation.Wpf/Extensions/ImageExtensions.cs:17

// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

using System;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Stride.Core.Annotations;

namespace Stride.Core.Presentation.Extensions
{
    public static class ImageExtensions
    {
        public static void SetSource([NotNull] this Image image, [NotNull] Uri uri)
        {
            if (image == null)
                throw new ArgumentNullException(nameof(image));
            if (uri == null)
                throw new ArgumentNullException(nameof(uri));

            image.Source = ImageSourceFromFile(uri);
        }

        public static void SetSource([NotNull] this Image image, [NotNull] string uri)
        {
            if (string.IsNullOrWhiteSpace(uri))
                throw new ArgumentException("Invalid 'uri' argument.");

            SetSource(image, new Uri(uri));
        }

        [NotNull]
        public static ImageSource ImageSourceFromFile([NotNull] Uri uri)
        {
            if (uri == null)

View on GitHub (pinned to 96fad776d2)