dotnet/wpf · error · ArgumentNullException

ArgumentNullException("textRunProperties.Typeface")

Error message

ArgumentNullException("textRunProperties.Typeface")

What it means

The TextEndOfLine constructor accepts null textRunProperties, but if provided, its Typeface must not be null — otherwise ArgumentNullException("textRunProperties.Typeface") is thrown. TextEndOfLine represents line-break runs, and a non-null properties object must carry a usable typeface.

Solutions

  1. Either pass a fully initialized TextRunProperties (Typeface set) or pass null instead of an empty properties object
  2. Set props.Typeface = new Typeface("Segoe UI") (or the paragraph's typeface) before constructing TextEndOfLine
  3. Fix custom TextRunProperties to always initialize Typeface in the constructor
  4. Guard in the TextSource: only construct TextEndOfLine when props?.Typeface != null, else pass null

Example fix

// before
var props = new CustomTextRunProperties(); // Typeface null
return new TextEndOfLine(1, props);
// after
return new TextEndOfLine(1, null); // or set props.Typeface = new Typeface("Segoe UI");
Defensive patterns

Strategy: type-guard

Validate before calling

if (props != null && props.Typeface == null) props = null; // or set a default typeface

Type guard

bool IsUsableProps(TextRunProperties p) => p == null || p.Typeface != null;

Try / catch

try { return new TextEndOfLine(1, props); }
catch (ArgumentNullException ex) { log.Warn("EndOfLine props had null Typeface; retrying with null", ex); return new TextEndOfLine(1, null); }

Prevention

When it happens

Trigger: Constructing TextEndOfLine (or subclass TextEndOfParagraph) with a TextRunProperties instance whose Typeface is null — e.g. a partially initialized custom TextRunProperties.

Common situations: Custom TextSource returning TextEndOfLine with freshly created run properties where Typeface was left unset; copying properties from a run whose typeface failed to resolve to null.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/439c3223bd99c5e4. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/TextEndOfLine.cs:46

        /// <param name="length">number of characters</param>
        public TextEndOfLine(int length) : this(length, null)
        {}


        /// <summary>
        /// Construct a linebreak run
        /// </summary>
        /// <param name="length">number of characters</param>
        /// <param name="textRunProperties">linebreak text run properties</param>
        public TextEndOfLine(
            int                 length, 
            TextRunProperties   textRunProperties
            )
        {
            ArgumentOutOfRangeException.ThrowIfNegativeOrZero(length);

            if (textRunProperties != null && textRunProperties.Typeface == null)
                throw new ArgumentNullException("textRunProperties.Typeface");

            _length = length;
            _textRunProperties = textRunProperties;
        }

        #endregion


        /// <summary>
        /// Reference to character buffer
        /// </summary>
        public sealed override CharacterBufferReference CharacterBufferReference
        {
            get { return new CharacterBufferReference(); }
        }

        
        /// <summary>

View on GitHub (pinned to 81131a70a4)