stride3d/stride · error · ArgumentNullException

font

Error message

font

What it means

Thrown by UIBatch.DrawString when the font parameter is null. DrawString requires a valid SpriteFont to render text, so a null font is rejected with ArgumentNullException before the string proxy is built. (The same method also throws for a null text argument.)

Solutions

  1. Assign a valid SpriteFont to the text element (e.g. in the UI editor or via code) before drawing.
  2. Null-check font at the call site and skip rendering or substitute a default font.
  3. Verify the font asset loads successfully and is not null after load.

Example fix

// before
uiBatch.DrawString(textElement.Font, textElement.Text, ref drawCommand);
// after
if (textElement.Font != null && textElement.Text != null)
    uiBatch.DrawString(textElement.Font, textElement.Text, ref drawCommand);
Defensive patterns

Strategy: validation

Validate before calling

if (font == null || string.IsNullOrEmpty(text)) return;

Type guard

bool CanDrawText(SpriteFont f, string s) => f != null && !string.IsNullOrEmpty(s);

Try / catch

try { uiBatch.DrawString(font, text, ref cmd); }
catch (ArgumentNullException e) when (e.ParamName == "font" || e.ParamName == "text") { log.Warning("DrawString skipped", e); }

Prevention

When it happens

Trigger: Calling DrawString(null, text, ref drawCommand) — commonly a UI text element whose Font property was never assigned or whose font asset failed to load.

Common situations: Missing font asset reference in a scene/UI component; asynchronous font load not completed; serialized field left unset; font deleted from assets.

Related errors


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

Appendix: source

Thrown at sources/engine/Stride.Graphics/UIBatch.cs:431

                DepthBias = depthBias,
                ColorScale = color,
                ColorAdd = Color.Zero,
                Swizzle = swizzle,
                Primitive = PrimitiveType.Rectangle,
                VertexShift = Vector4.Zero,
                UnitXWorld = worldViewProjectionMatrix.Row1,
                UnitYWorld = worldViewProjectionMatrix.Row2,
                LeftTopCornerWorld = worldViewProjectionMatrix.Row4,
            };

            var elementInfo = new ElementInfo(4, 6, in drawInfo, depthBias);

            Draw(texture, in elementInfo);
        }

        internal void DrawString([NotNull] SpriteFont font, [NotNull] string text, ref SpriteFont.InternalUIDrawCommand drawCommand)
        {
            if (font == null) throw new ArgumentNullException(nameof(font));
            if (text == null) throw new ArgumentNullException(nameof(text));

            var proxy = new SpriteFont.StringProxy(text);

            // shift the string position so that it is written from the left/top corner of the element
            var leftTopCornerOffset = drawCommand.TextBoxSize / 2;
            var worldMatrix = drawCommand.Matrix;
            worldMatrix.M41 -= worldMatrix.M11 * leftTopCornerOffset.X + worldMatrix.M21 * leftTopCornerOffset.Y;
            worldMatrix.M42 -= worldMatrix.M12 * leftTopCornerOffset.X + worldMatrix.M22 * leftTopCornerOffset.Y;
            worldMatrix.M43 -= worldMatrix.M13 * leftTopCornerOffset.X + worldMatrix.M23 * leftTopCornerOffset.Y;

            // transform the world matrix into the world view project matrix
            Matrix.Multiply(ref worldMatrix, ref viewProjectionMatrix, out drawCommand.Matrix);

            font.TypeSpecificRatios(drawCommand.RequestedFontSize, ref drawCommand.SnapText, ref drawCommand.RealVirtualResolutionRatio, out var actualFontSize);

            // snap draw start position to prevent characters to be drawn in between two pixels
            if (drawCommand.SnapText)

View on GitHub (pinned to 96fad776d2)