dotnet/wpf · error

Specified index is out of range or child at index is null…

Error message

Specified index is out of range or child at index is null. Do not call this method if VisualChildrenCount returns zero, indicating that the Visual has no children.

What it means

BulletDecorator.GetVisualChild(index) validates that the requested index is within [0, VisualChildrenCount-1] and throws ArgumentOutOfRangeException otherwise. It can return only the _bullet child (index 0 when a bullet exists); requests outside that range are invalid calls into the visual tree API.

Solutions

  1. Check VisualChildrenCount before calling GetVisualChild and clamp the index
  2. Iterate children with VisualTreeHelper.GetChild only up to VisualChildrenCount-1
  3. Use VisualTreeHelper/LogicalTreeHelper traversal instead of calling GetVisualChild directly

Example fix

// before
var child = bulletDecorator.GetVisualChild(1);
// after
if (bulletDecorator.VisualChildrenCount > 1)
    var child = bulletDecorator.GetVisualChild(1);
Defensive patterns

Strategy: validation

Validate before calling

if (decorator != null && index >= 0 && index < decorator.VisualChildrenCount) { var c = decorator.GetVisualChild(index); }

Type guard

static bool CanGetVisualChild(System.Windows.Media.Visual v, int i) => i >= 0 && i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(v);

Try / catch

try { var child = decorator.GetVisualChild(index); }
catch (ArgumentOutOfRangeException) { /* treat as no child at index */ }

Prevention

When it happens

Trigger: Calling GetVisualChild with a negative index, an index >= VisualChildrenCount, or index 0 when there is no bullet (Bullet is null and only the Decorator child is present).

Common situations: Custom code walking the visual tree with wrong assumptions about BulletDecorator's child count, or calling GetVisualChild when VisualChildrenCount is zero.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/BulletDecorator.cs:217

            }
        }

        /// <summary>
        /// Returns the Visual children count.
        /// </summary>
        protected override int VisualChildrenCount
        {
            get { return (Child == null ? 0 : 1) + (_bullet == null ? 0 : 1); }
        }

        /// <summary>
        /// Returns the child at the specified index.
        /// </summary>
        protected override Visual GetVisualChild(int index)
        {
            if (index < 0 || index > VisualChildrenCount-1)
            {
                throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);
            }

            if (index == 0 && _bullet != null)
            {
                return _bullet;
            }

            return Child;
        }
        /// <summary>
        /// Updates DesiredSize of the BulletDecorator. Called by parent UIElement.
        /// This is the first pass of layout.
        /// </summary>
        /// <param name="constraint">Constraint size is an "upper limit" that BulletDecorator should not exceed.</param>
        /// <returns>BulletDecorator' desired size.</returns>
        protected override Size MeasureOverride(Size constraint)
        {
                Size bulletSize = new Size();

View on GitHub (pinned to 81131a70a4)