dotnet/wpf · error · ArgumentNullException

ArgumentNullException("textRunProperties.CultureInfo")

Error message

ArgumentNullException("textRunProperties.CultureInfo")

What it means

The TextCharacters constructor also requires textRunProperties.CultureInfo to be non-null, throwing ArgumentNullException("textRunProperties.CultureInfo") otherwise. CultureInfo drives script-specific shaping and digit substitution for the run.

Solutions

  1. Set TextRunProperties.CultureInfo (e.g. CultureInfo.CurrentCulture or new CultureInfo("en-US")) before constructing TextCharacters
  2. Fix the custom TextRunProperties subclass to initialize CultureInfo in its constructor
  3. Fall back to CultureInfo.InvariantCulture or CultureInfo.DefaultThreadCurrentCulture when no specific culture is known
  4. Copy culture from the containing TextParagraphProperties when creating runs

Example fix

// before
props.CultureInfo = someCulture; // someCulture is null
return new TextCharacters(text, 0, text.Length, props);
// after
props.CultureInfo = someCulture ?? CultureInfo.CurrentCulture;
return new TextCharacters(text, 0, text.Length, props);
Defensive patterns

Strategy: type-guard

Validate before calling

if (props?.CultureInfo == null) props.CultureInfo = CultureInfo.CurrentCulture;

Type guard

bool HasValidCulture(TextRunProperties p) => p?.CultureInfo != null;

Try / catch

try { return new TextCharacters(text, 0, text.Length, props); }
catch (ArgumentNullException ex) when (ex.ParamName == "textRunProperties.CultureInfo") { props.CultureInfo = CultureInfo.CurrentCulture; return new TextCharacters(text, 0, text.Length, props); }

Prevention

When it happens

Trigger: Constructing TextCharacters with TextRunProperties whose CultureInfo is null — typically a custom TextRunProperties implementation that sets Typeface but not CultureInfo, or CultureInfo taken from an uninitialized lookup.

Common situations: Custom TextSource implementations; culture derived from Thread.CurrentThread.CurrentCulture in a context where it is null; cloning run properties and dropping the culture.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/TextCharacters.cs:119

        /// 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
        
        /// <summary>
        /// Character buffer
        /// </summary>
        public sealed override CharacterBufferReference CharacterBufferReference 

View on GitHub (pinned to 81131a70a4)