{"record":{"id":"731f1887cafda06d","repo":"stride3d/stride","slug":"nameof-heightmap-bytes-is-a-null","errorCode":null,"errorMessage":"{ nameof(Heightmap.Bytes) } is a null.","messagePattern":"(.+?) is a null\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Physics/HeightStickArraySourceFromHeightmap.cs","lineNumber":57,"sourceCode":"            var heightStickArrayLength = heightStickArray.Length - index;\n            if (heightStickArrayLength <= 0) throw new IndexOutOfRangeException(nameof(index));\n\n            var typeOfT = typeof(T);\n            T[] heights;\n\n            if (typeOfT == typeof(float))\n            {\n                if (Heightmap.Floats == null) throw new InvalidOperationException($\"{ nameof(Heightmap.Floats) } is a null.\");\n                heights = (T[])(object)Heightmap.Floats;\n            }\n            else if (typeOfT == typeof(short))\n            {\n                if (Heightmap.Shorts == null) throw new InvalidOperationException($\"{ nameof(Heightmap.Shorts) } is a null.\");\n                heights = (T[])(object)Heightmap.Shorts;\n            }\n            else if (typeOfT == typeof(byte))\n            {\n                if (Heightmap.Bytes == null) throw new InvalidOperationException($\"{ nameof(Heightmap.Bytes) } is a null.\");\n                heights = (T[])(object)Heightmap.Bytes;\n            }\n            else\n            {\n                throw new NotSupportedException($\"{ typeof(UnmanagedArray<T>) } type is not supported.\");\n            }\n\n            var heightsLength = heights.Length;\n            if (heightStickArrayLength < heightsLength) throw new ArgumentException($\"{ nameof(heightStickArray) }.{ nameof(heightStickArray.Length) } is not enough to copy.\");\n\n            heightStickArray.Write(heights, index * Unsafe.SizeOf<T>(), 0, heightsLength);\n        }\n\n        public bool Match(object obj)\n        {\n            var other = obj as HeightStickArraySourceFromHeightmap;\n\n            if (other == null)","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Physics/HeightStickArraySourceFromHeightmap.cs#L39-L75","documentation":"CopyTo<T> on HeightStickArraySourceFromHeightmap requires the source Heightmap to actually hold data in the buffer matching the requested element type T. When T is byte, it reads Heightmap.Bytes; if that array is null it throws InvalidOperationException, because there is no byte data to write into the physics heightstick array.","triggerScenarios":"Calling CopyTo<byte>(...) on a heightstick source whose Heightmap has HeightfieldTypes byte data not yet allocated, or whose Bytes array was never populated (only Shorts or Floats set).","commonSituations":"Constructing a Heightmap deserialized from an asset where the byte buffer failed to load; mixing up HeightfieldTypes (heightmap is Short/Float but code copies as byte).","solutions":["Ensure the Heightmap is of byte type and its Bytes array is allocated and populated before calling CopyTo<byte>.","Check Heightmap.HeightType (or which buffer is non-null) and call CopyTo with the matching type parameter (short/byte/float).","If data comes from an asset, verify it loaded successfully before building the physics heightstick array."],"exampleFix":"// before\nvar source = new HeightStickArraySourceFromHeightmap { Heightmap = heightmap };\nsource.CopyTo<byte>(stickArray, 0); // throws if heightmap.Bytes is null\n// after\nif (heightmap.Bytes == null)\n    throw new InvalidOperationException(\"Heightmap byte data must be populated before CopyTo<byte>.\");\nsource.CopyTo<byte>(stickArray, 0);","handlingStrategy":"validation","validationCode":"if (heightmap.Bytes == null || heightmap.HeightType != HeightfieldTypes.Byte)\n    throw new InvalidOperationException(\"Heightmap byte data must be populated before CopyTo<byte>.\");","typeGuard":"bool CanCopyAsByte(var h) => h?.Bytes != null;","tryCatchPattern":null,"preventionTips":["Always populate the heightmap buffer matching HeightType before creating heightstick arrays.","Check which of Shorts/Bytes is non-null and pick the CopyTo generic argument from that.","Validate loaded assets before wiring them into physics."],"tags":["physics","null-reference","heightmap"],"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"}