dotnet/wpf · error · ArgumentNullException
ArgumentNullException("textRunProperties.Typeface")
Error message
ArgumentNullException("textRunProperties.Typeface") What it means
The TextCharacters constructor requires textRunProperties and specifically its Typeface to be non-null; if textRunProperties.Typeface is null it throws ArgumentNullException("textRunProperties.Typeface"). The typeface is essential to shape and render the character run.
Solutions
- Set TextRunProperties.Typeface to a valid Typeface before creating TextCharacters (e.g. new Typeface("Segoe UI"))
- Fix a custom TextRunProperties subclass so Typeface is assigned in its constructor
- If a font family failed to resolve, fall back to a default: props.Typeface = new Typeface("Arial")
- Null-check the typeface in the TextSource before constructing the run
Example fix
// before
var props = new CustomTextRunProperties(); // Typeface never set
return new TextCharacters(text, 0, text.Length, props);
// after
var props = new CustomTextRunProperties
{
Typeface = new Typeface("Segoe UI"),
FontRenderingEmSize = 12
};
return new TextCharacters(text, 0, text.Length, props); Defensive patterns
Strategy: type-guard
Validate before calling
if (props?.Typeface == null) props.Typeface = new Typeface("Segoe UI"); Type guard
bool HasValidTypeface(TextRunProperties p) => p?.Typeface != null;
Try / catch
try { return new TextCharacters(text, 0, text.Length, props); }
catch (ArgumentNullException ex) { log.Error("Run properties missing Typeface", ex); throw; } Prevention
- Initialize Typeface in custom TextRunProperties constructors
- Fall back to a default Typeface when font resolution fails
- Null-check run properties in TextSource.GetTextRun before building runs
When it happens
Trigger: Constructing TextCharacters with a TextRunProperties whose Typeface property is null — e.g. a custom TextRunProperties subclass that never sets Typeface, or a default-constructed instance.
Common situations: Custom text source (TextSource.GetTextRun) returning TextCharacters with hand-rolled run properties; forgetting to set Typeface when building runs programmatically; a font lookup returning null and being stored into Typeface.
Related errors
- ArgumentNullException("characterBufferReference.CharacterBuf…
- ArgumentNullException("textRunProperties.CultureInfo")
- ArgumentNullException("textRunProperties.Typeface")
- paragraphProperties.DefaultTextRunProperties.Typeface
- annotation component
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/6bcb7ec8d67dc5c2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/TextCharacters.cs:114
)
{}
/// <summary>
/// Internal constructor of TextContent
/// </summary>
private TextCharacters(
CharacterBufferReference characterBufferReference,
int length,
TextRunProperties textRunProperties
)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(length);
ArgumentNullException.ThrowIfNull(textRunProperties);
if (textRunProperties.Typeface == null)
{
throw new ArgumentNullException("textRunProperties.Typeface");
}
if (textRunProperties.CultureInfo == null)
{
throw new ArgumentNullException("textRunProperties.CultureInfo");
}
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(textRunProperties.FontRenderingEmSize, "textRunProperties.FontRenderingEmSize");
_characterBufferReference = characterBufferReference;
_length = length;
_textRunProperties = textRunProperties;
}
#endregion
#region TextRun implementationView on GitHub (pinned to 81131a70a4)