stride3d/stride · error · ArgumentNullException

ArgumentNullException: heightmap

Error message

ArgumentNullException: heightmap

What it means

The HeightmapExtensions.IsValid extension method validates the heightmap's height data integrity, but first asserts the heightmap instance itself is non-null, throwing ArgumentNullException('heightmap'). Stride uses this fail-fast guard so callers do not silently treat a missing heightmap as 'valid' or 'invalid'.

Solutions

  1. Null-check the Heightmap before calling IsValid: if (heightmap != null && heightmap.IsValid()).
  2. Ensure terrain/asset loading succeeded and the HeightmapData field was populated before validation.
  3. Treat a null heightmap explicitly in game logic (e.g. generate a default flat heightmap) instead of passing null into extension checks.
  4. Wrap the validation call in try-catch for ArgumentNullException when the heightmap source is untrusted.

Example fix

// before
if (heightmap.IsValid()) { ... } // throws if heightmap is null

// after
if (heightmap != null && heightmap.IsValid()) { ... }
Defensive patterns

Strategy: type-guard

Validate before calling

if (heightmap is null) return false; // or handle explicitly before calling IsValid
return heightmap.IsValid();

Type guard

bool TryValidate(Heightmap heightmap, out bool isValid)
{
    if (heightmap is null) { isValid = false; return false; }
    isValid = heightmap.IsValid();
    return true;
}

Try / catch

try { valid = heightmap.IsValid(); }
catch (ArgumentNullException ex) when (ex.ParamName == "heightmap") { valid = false; }

Prevention

When it happens

Trigger: Calling heightmap.IsValid() where the receiver is null — e.g. the heightmap was never assigned, a terrain component's HeightmapData is null, or an asset failed to load and produced a null reference that was then checked for validity.

Common situations: Checking validity of a heightmap loaded from an asset that failed to deserialize; validating optional heightmap data before simulation setup; pipeline code that passes a null heightmap from a failed import.

Related errors


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

Appendix: source

Thrown at sources/engine/Stride.Physics/Engine/HeightmapExtensions.cs:15

// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Linq;
using Stride.Core.Annotations;
using Stride.Core.Mathematics;
using Stride.Graphics;

namespace Stride.Physics
{
    public static class HeightmapExtensions
    {
        public static bool IsValid([NotNull] this Heightmap heightmap)
        {
            if (heightmap == null) throw new ArgumentNullException(nameof(heightmap));

            bool IsValidHeights()
            {
                var length = heightmap.Size.X * heightmap.Size.Y;

                switch (heightmap.HeightType)
                {
                    case HeightfieldTypes.Float when heightmap.Floats != null && heightmap.Floats.Length == length:
                        return true;

                    case HeightfieldTypes.Short when heightmap.Shorts != null && heightmap.Shorts.Length == length:
                        return true;

                    case HeightfieldTypes.Byte when heightmap.Bytes != null && heightmap.Bytes.Length == length:
                        return true;
                }

                return false;

View on GitHub (pinned to 96fad776d2)