stride3d/stride · error · ArgumentNullException

ArgumentNullException: data

Error message

ArgumentNullException: data

What it means

Heightmap.Create validates that the data array parameter is non-null before building the heightmap. Passing a null data array throws ArgumentNullException with the parameter name 'data'. This is a fail-fast guard against constructing a heightmap with no height data.

Solutions

  1. Ensure the data array is allocated and populated before calling Heightmap.Create.
  2. Check for null at the call site: if (data == null) { /* load or generate */ }
  3. If data comes from asset deserialization, verify the asset loaded successfully before creating the heightmap.
  4. For procedural terrain, initialize the array to the correct size (size.X * size.Y) before calling Create.

Example fix

// before
var heightmap = Heightmap.Create(size, HeightfieldTypes.Float, range, 1f, data); // data may be null

// after
if (data == null)
    data = new float[size.X * size.Y];
var heightmap = Heightmap.Create(size, HeightfieldTypes.Float, range, 1f, data);
Defensive patterns

Strategy: validation

Validate before calling

if (data == null) throw new InvalidOperationException("Heightmap data must be loaded before Heightmap.Create.");

Type guard

bool HasHeightData<T>(T[] data) => data is { Length: > 0 };

Try / catch

try { var hm = Heightmap.Create(size, type, range, scale, data); }
catch (ArgumentNullException ex) when (ex.ParamName == "data") { Logger.Error("Heightmap data array was null"); }

Prevention

When it happens

Trigger: Calling Heightmap.Create<T>(size, heightType, heightRange, heightScale, data) with data == null, e.g. from HeightmapDeserialization failing, a data member not yet populated, or code that computes the array conditionally and skips initialization.

Common situations: Loading terrain assets where the raw height array failed to deserialize; procedurally generating heightmaps and forgetting to fill the array; passing a field that defaults to null before asset load completes.

Related errors


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

Appendix: source

Thrown at sources/engine/Stride.Physics/Engine/Heightmap.cs:76

        /// The range of the height.
        /// </summary>
        /// <remarks>
        /// X is min height and Y is max height.
        /// (height * HeightScale) should be in this range.
        /// Positive and negative heights can not be handle at the same time when the height type is Byte.
        /// </remarks>
        [DataMember(60)]
        public Vector2 HeightRange;

        /// <summary>
        /// Used to calculate the height when the height type is Short or Byte. HeightScale should be 1 when the height type is Float.
        /// </summary>
        [DataMember(70)]
        public float HeightScale;

        public static Heightmap Create<T>(Int2 size, HeightfieldTypes heightType, Vector2 heightRange, float heightScale, T[] data)
        {
            if (data == null) throw new ArgumentNullException(nameof(data));

            HeightmapUtils.CheckHeightParameters(size, heightType, heightRange, heightScale, true);

            var length = size.X * size.Y;

            switch (data)
            {
                case float[] floats when floats.Length == length: break;
                case short[] shorts when shorts.Length == length: break;
                case byte[] bytes when bytes.Length == length: break;
                default: throw new ArgumentException($"{ typeof(T[]) } is not supported in { heightType } height type. Or { nameof(data) }.{ nameof(data).Length } doesn't match { nameof(size) }.");
            }

            var heightmap = new Heightmap
            {
                HeightType = heightType,
                Size = size,
                HeightRange = heightRange,

View on GitHub (pinned to 96fad776d2)