dotnet/wpf · error · ArgumentNullException

block

Error message

block

What it means

The TextFlow(Block) constructor throws ArgumentNullException when the caller passes a null block. The initial block added to the TextFlow's Blocks collection is a required argument; a TextFlow without any initial block should use the parameterless constructor instead.

Solutions

  1. Ensure the Block (Paragraph, Section, etc.) is created before constructing TextFlow
  2. Add a null check or throw a descriptive error at the call site
  3. Check why the Block reference is null (binding failure, failed resource lookup) and fix the root cause

Example fix

// before
Block block = FindBlock(name); // may return null
var flow = new TextFlow(block);

// after
Block block = FindBlock(name) ?? new Paragraph(new Run(string.Empty));
var flow = new TextFlow(block);
Defensive patterns

Strategy: validation

Validate before calling

if (block == null) block = new Paragraph(new Run(string.Empty));
var flow = new TextFlow(block);

Type guard

bool isUsableBlock(Block b) => b != null;

Try / catch

try { flow = new TextFlow(block); } catch (ArgumentNullException) { flow = new TextFlow(new Paragraph()); }

Prevention

When it happens

Trigger: new TextFlow(null) or passing a Block field/property that was never assigned (e.g., a Failed binding or unset FlowDocument block).

Common situations: Programmatic document construction where a Paragraph/Section variable is null; XAML-load fallback paths where the block resource did not resolve; refactoring left a Block property uninitialized.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextFlow.cs:302

        /// </summary>
        public TextFlow() 
            : base()
        {
            Initialize();
        }

        /// <summary>
        /// TextFlow constructor.
        /// </summary>
        /// <param name="block">
        /// Block initially added to TextFlow's Blocks collection.
        /// </param>
        public TextFlow(Block block)
            : base()
        {
            if (block == null)
            {
                throw new ArgumentNullException("block");
            }

            Initialize();

            this.Blocks.Add(block);
        }

        #endregion Constructors

        //-------------------------------------------------------------------
        //
        //  Public Methods
        //
        //-------------------------------------------------------------------

        #region Public Methods

        /// <summary>

View on GitHub (pinned to 81131a70a4)