stride3d/stride · error · ArgumentNullException
text
Error message
text
What it means
SpriteFont.MeasureString(string, ref Vector2, int) throws ArgumentNullException when the text string is null. Measuring requires iterating the characters to compute the layout size; null is rejected at the entry point. An empty string is valid and yields a zero-size measurement.
Solutions
- Pass string.Empty for absent text — MeasureString(empty) returns a zero-size Vector2
- Guard: if (text != null) var size = font.MeasureString(text, ...)
- Coalesce with ?? string.Empty at the call site
- Fix the text provider (localization, data binding) so it never returns null
Example fix
// before var size = font.MeasureString(text, ref fontSize, text.Length); // text may be null // after var size = font.MeasureString(text ?? string.Empty, ref fontSize, (text ?? string.Empty).Length);
Defensive patterns
Strategy: validation
Validate before calling
var safeText = text ?? string.Empty; var size = font.MeasureString(safeText, ref fontSize, safeText.Length);
Type guard
static bool CanMeasure(string s) => s != null;
Try / catch
try { size = font.MeasureString(text, ref fontSize, len); }
catch (ArgumentNullException) { size = Vector2.Zero; } Prevention
- Default all text sources to empty strings
- Make localization providers return non-null values
- Coalesce before measuring when text comes from data binding
When it happens
Trigger: Calling MeasureString with a null string, e.g. measuring text loaded from a data source that returned null, or forwarding an optional UI label value before it is set.
Common situations: Localization lookup returning null for a missing key; measuring a player/ itemName that is null until data loads; computing layout for a label whose Text property was never initialized.
Related errors
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/0b3f442ab6e3cd92.
Report an issue: GitHub.
Appendix: source
Thrown at sources/engine/Stride.Graphics/SpriteFont.cs:446
/// <param name="fontSize">The size of the font (ignored in the case of static fonts)</param>
/// <param name="length">The length of the string to measure</param>
/// <returns>Vector2.</returns>
public Vector2 MeasureString([NotNull] StringBuilder text, Vector2 fontSize, int length)
{
return MeasureString(text, ref fontSize, length);
}
/// <summary>
/// Returns the width and height of the provided text for a given font size
/// </summary>
/// <param name="text">The string to measure.</param>
/// <param name="fontSize">The size of the font (ignored in the case of static fonts)</param>
/// <param name="length">The length of the string to measure</param>
/// <returns>Vector2.</returns>
public Vector2 MeasureString([NotNull] string text, ref Vector2 fontSize, int length)
{
if (text == null)
throw new ArgumentNullException(nameof(text));
var proxy = new StringProxy(text, length);
return MeasureString(proxy, fontSize);
}
/// <summary>
/// Returns the width and height of the provided text for a given font size
/// </summary>
/// <param name="text">The string to measure.</param>
/// <param name="fontSize">The size of the font (ignored in the case of static fonts)</param>
/// <param name="length">The length of the string to measure</param>
/// <returns>Vector2.</returns>
public Vector2 MeasureString([NotNull] StringBuilder text, ref Vector2 fontSize, int length)
{
if (text == null)
throw new ArgumentNullException(nameof(text));
var proxy = new StringProxy(text, length);View on GitHub (pinned to 96fad776d2)