stride3d/stride · error · ArgumentNullException
text
Error message
text
What it means
UIBatch.DrawString throws ArgumentNullException when either the SpriteFont or the text string passed to it is null. The library requires a valid font and a non-null string to render text into the UI batch; null values cannot be drawn. This is a fast-fail guard placed at the start of the draw call so failures surface at the call site rather than deep inside batch rendering.
Solutions
- Ensure the SpriteFont is loaded before drawing: font = Content.Load<SpriteFont>("MyFont") and verify it is not null
- Replace null text with string.Empty or a placeholder before calling DrawString
- Add an early guard in the calling code: if (font == null || text == null) return; or throw a descriptive error
- Check that the font asset is included in the project's asset compilation so loading succeeds
Example fix
// before
batch.DrawString(font, labelText, ref drawCommand);
// after
if (font != null && labelText != null)
batch.DrawString(font, labelText, ref drawCommand); Defensive patterns
Strategy: type-guard
Validate before calling
if (font == null) throw new InvalidOperationException("UI font not loaded");
if (text == null) text = string.Empty; Type guard
static bool CanDraw(SpriteFont font, string text) => font != null && text != null;
Try / catch
try { batch.DrawString(font, text, ref cmd); }
catch (ArgumentNullException ex) { logger.Warn($"Skipped text draw: {ex.ParamName} is null"); } Prevention
- Load fonts during initialization and fail fast if Content.Load returns null
- Use string.Empty as the default for UI label fields instead of null
- Enable nullable reference types to catch unassigned font fields at compile time
When it happens
Trigger: Calling DrawString(spriteFont, text, ref drawCommand) with a font variable that was never loaded (null) or with a text argument that is null, e.g. reading a UI label from a data source that returned null.
Common situations: Font assets missing from the build/content database so the font field stays null; a game data table or localization file with an absent value deserialized as null; refactoring away an initialization that used to assign the font.
Related errors
- Invalid Z slice index
- MipLevels must be <=
- Width/Height/Depth must be power of 2
- Image format not supported
- This file format is not yet implemented.
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/db19c9634e93dd08.
Report an issue: GitHub.
Appendix: source
Thrown at sources/engine/Stride.Graphics/UIBatch.cs:432
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)