dotnet/wpf · error · ArgumentNullException

new ArgumentNullException(name)

Error message

new ArgumentNullException(name)

What it means

ValidateRangeArgument throws ArgumentNullException when a TextPatternRange parameter passed to Compare, CompareEndpoints, or MoveEndpointByRange is null. The methods require a second valid range obtained from the same TextPattern instance.

Solutions

  1. Null-check the TextPatternRange argument before invoking Compare/CompareEndpoints/MoveEndpointByRange.
  2. Ensure the range was successfully created via TextPattern.DocumentRange/GetSelection/RangeFromChild and not null.
  3. If the source range may be stale, re-acquire it from a live AutomationElement.

Example fix

// before
if (range.Compare(otherRange)) { ... } // otherRange may be null
// after
if (otherRange != null && range.Compare(otherRange)) { ... }
Defensive patterns

Strategy: validation

Validate before calling

if (otherRange == null) throw new ArgumentNullException(nameof(otherRange)); // or early-return/throw in caller before Compare/CompareEndpoints/MoveEndpointByRange

Type guard

static bool IsUsableRange(TextPatternRange r) => r is not null;

Try / catch

try { return range.CompareEndpoints(TextPatternRangeEndpoint.Start, otherRange, TextPatternRangeEndpoint.Start); }
catch (ArgumentNullException) { return int.MinValue; // or rethrow with context }

Prevention

When it happens

Trigger: Calling range.Compare(null), range.CompareEndpoints(TextPatternRangeEndpoint.Start, null, ...), or range.MoveEndpointByRange(..., null) — typically when the other range came from a failed lookup, a disconnected element, or an uninitialized variable.

Common situations: Ranges stored in collections where entries may be null; earlier provider calls returning null (element vanished); refactoring that forgot to initialize the target range.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/Text/TextRange.cs:449

        #region Private Methods

        // check an endpoint argument to see if it is valid.
        private void ValidateEndpointArgument(TextPatternRangeEndpoint endpoint, string name)
        {
            if (endpoint != TextPatternRangeEndpoint.Start && endpoint != TextPatternRangeEndpoint.End)
            {
                Misc.ThrowInvalidArgument(name);
            }
        }

        // check a range argument to see if it is valid.
        private void ValidateRangeArgument(TextPatternRange range, string name)
        {
            // check if the argument is null
            if (range == null)
            {
                throw new ArgumentNullException(name);
            }

            // check if the range comes from a different text pattern.
            if (!TextPattern.Compare(_pattern, range._pattern))
            {
                Misc.ThrowInvalidArgument(name);
            }
}

        // check an unit argument to see if it is valid.
        private void ValidateUnitArgument(TextUnit unit, string name)
        {
            if (unit<TextUnit.Character || unit>TextUnit.Document)
            {
                Misc.ThrowInvalidArgument(name);
            }
        }

View on GitHub (pinned to 81131a70a4)