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

  1. Set TextRunProperties.Typeface to a valid Typeface before creating TextCharacters (e.g. new Typeface("Segoe UI"))
  2. Fix a custom TextRunProperties subclass so Typeface is assigned in its constructor
  3. If a font family failed to resolve, fall back to a default: props.Typeface = new Typeface("Arial")
  4. 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

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


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 implementation

View on GitHub (pinned to 81131a70a4)