dotnet/wpf · error · ArgumentNullException

ArgumentNullException: hitTestParameters

Error message

ArgumentNullException: hitTestParameters

What it means

TextFlow.HitTestCore (the PointHitTestParameters overload) throws ArgumentNullException when hitTestParameters is null. Precise hit testing needs the hit point carried in the parameters to test against the rendered Rect.

Solutions

  1. Always construct PointHitTestParameters with a valid Point before hit testing
  2. Prefer public HitTest APIs (UIElement.HitTest/VisualTreeHelper.HitTest) which build parameters for you
  3. Null-check parameters in test/interop harnesses before invoking HitTestCore

Example fix

// before
var result = flow.HitTestCore(null);

// after
var result = flow.HitTestCore(new PointHitTestParameters(new Point(x, y)));
Defensive patterns

Strategy: validation

Validate before calling

if (hitTestParameters != null) { var r = flow.HitTestCore(hitTestParameters); }

Type guard

bool hasHitPoint(PointHitTestParameters p) => p != null;

Try / catch

try { result = flow.HitTestCore(p); } catch (ArgumentNullException) { result = null; }

Prevention

When it happens

Trigger: Calling HitTestCore(new PointHitTestParameters(...)) with null, or custom input/hit-test code invoking the protected method directly without constructing parameters.

Common situations: Custom hit-testing or input-simulation code paths; unit tests calling HitTestCore directly with null; interop layers that drop the parameters object.

Related errors


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

Appendix: source

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

        ///<param name="child">Reference to a child UIElement that had AffectsParentMeasure/AffectsParentArrange property invalidated.</param>
        protected internal sealed override void ParentLayoutInvalidated(UIElement child)
        {
            if (child == null)
            {
                throw new ArgumentNullException("child");
            }

            OnChildUIElementChanged(child);
        }

        /// <summary>
        /// HitTestCore implements precise hit testing against render contents
        /// </summary>
        protected sealed override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
        {
            if (hitTestParameters == null)
            {
                throw new ArgumentNullException("hitTestParameters");
            }

            Rect r = new Rect(new Point(), RenderSize);

            if (r.Contains(hitTestParameters.HitPoint))
            {
                return new PointHitTestResult(this, hitTestParameters.HitPoint);
            }

            return null;
        }

        /// <summary>
        ///     Hit tests to the correct ContentElement 
        ///     within the ContentHost that the mouse 
        ///     is over
        /// </summary>
        /// <param name="point">

View on GitHub (pinned to 81131a70a4)