stride3d/stride · error · ArgumentNullException
spriteFont
Error message
spriteFont
What it means
SpriteBatch.MeasureString(SpriteFont, string, Vector2?) measures text size using the given SpriteFont. A null spriteFont throws ArgumentNullException because font metrics are required to compute the layout.
Solutions
- Ensure the SpriteFont is loaded before measuring text
- Guard the call with a null check or default font
- Fix font asset references/build inclusion
Example fix
// before var size = spriteBatch.MeasureString(font, text); // after var size = font != null ? spriteBatch.MeasureString(font, text) : Vector2.Zero;
Defensive patterns
Strategy: type-guard
Validate before calling
if (spriteFont == null) return Vector2.Zero;
Type guard
bool HasFont(SpriteFont f) => f != null;
Try / catch
try { size = batch.MeasureString(font, text); } catch (ArgumentNullException) { size = Vector2.Zero; } Prevention
- Load SpriteFont assets before UI layout passes
- Use a default/fallback font when the primary fails to load
When it happens
Trigger: Calling MeasureString(null, text) — e.g. a font asset that failed to load or a null font field passed through from UI code.
Common situations: SpriteFont asset not present in build output, failed content loading, or unassigned font references in game/UI configuration.
Related errors
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/cc04475839654d15.
Report an issue: GitHub.
Appendix: source
Thrown at sources/engine/Stride.Graphics/SpriteBatch.cs:318
/// <param name="layerDepth">The depth of a layer. By default, 0 represents the front layer and 1 represents a back layer. Use SpriteSortMode if you want sprites to be sorted during drawing.</param>
public void Draw(Texture texture, Vector2 position, RectangleF? sourceRectangle, Color4 color, float rotation,
Vector2 origin, Vector2 scale, SpriteEffects effects = SpriteEffects.None, ImageOrientation orientation = ImageOrientation.AsIs, float layerDepth = 0, Color4 colorAdd = default(Color4))
{
var destination = new RectangleF(position.X, position.Y, scale.X, scale.Y);
DrawSprite(texture, ref destination, true, ref sourceRectangle, color, colorAdd, rotation, ref origin, effects, orientation, layerDepth);
}
/// <summary>
/// Measure the size of the given text in virtual pixels depending on the target size.
/// </summary>
/// <param name="spriteFont">The font used to draw the text.</param>
/// <param name="text">The text to measure.</param>
/// <param name="targetSize">The size of the target to render in. If null, the size of the window back buffer is used.</param>
/// <returns>The size of the text in virtual pixels.</returns>
/// <exception cref="ArgumentNullException">The provided sprite font is null.</exception>
public Vector2 MeasureString(SpriteFont spriteFont, string text, Vector2? targetSize = null)
{
if (spriteFont == null) throw new ArgumentNullException("spriteFont");
return MeasureString(spriteFont, text, spriteFont.Size, targetSize);
}
/// <summary>
/// Measure the size of the given text in virtual pixels depending on the target size.
/// </summary>
/// <param name="spriteFont">The font used to draw the text.</param>
/// <param name="text">The text to measure.</param>
/// <param name="fontSize">The font size (in pixels) used to draw the text.</param>
/// <param name="targetSize">The size of the target to render in. If null, the size of the window back buffer is used.</param>
/// <returns>The size of the text in virtual pixels.</returns>
/// <exception cref="ArgumentNullException">The provided sprite font is null.</exception>
public Vector2 MeasureString(SpriteFont spriteFont, string text, float fontSize, Vector2? targetSize = null)
{
if (spriteFont == null) throw new ArgumentNullException("spriteFont");
if (string.IsNullOrEmpty(text))View on GitHub (pinned to 96fad776d2)