{"record":{"id":"838c16a22dfeaa94","repo":"dotnet/wpf","slug":"specified-index-is-out-of-range-or-child-at-index-is-null-do-838c16","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/BulletDecorator.cs","lineNumber":217,"sourceCode":"            }\n        }\n\n        /// <summary>\n        /// Returns the Visual children count.\n        /// </summary>\n        protected override int VisualChildrenCount\n        {\n            get { return (Child == null ? 0 : 1) + (_bullet == null ? 0 : 1); }\n        }\n\n        /// <summary>\n        /// Returns the child at the specified index.\n        /// </summary>\n        protected override Visual GetVisualChild(int index)\n        {\n            if (index < 0 || index > VisualChildrenCount-1)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);\n            }\n\n            if (index == 0 && _bullet != null)\n            {\n                return _bullet;\n            }\n\n            return Child;\n        }\n        /// <summary>\n        /// Updates DesiredSize of the BulletDecorator. Called by parent UIElement.\n        /// This is the first pass of layout.\n        /// </summary>\n        /// <param name=\"constraint\">Constraint size is an \"upper limit\" that BulletDecorator should not exceed.</param>\n        /// <returns>BulletDecorator' desired size.</returns>\n        protected override Size MeasureOverride(Size constraint)\n        {\n                Size bulletSize = new Size();","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/BulletDecorator.cs#L199-L235","documentation":"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.","triggerScenarios":"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).","commonSituations":"Custom code walking the visual tree with wrong assumptions about BulletDecorator's child count, or calling GetVisualChild when VisualChildrenCount is zero.","solutions":["Check VisualChildrenCount before calling GetVisualChild and clamp the index","Iterate children with VisualTreeHelper.GetChild only up to VisualChildrenCount-1","Use VisualTreeHelper/LogicalTreeHelper traversal instead of calling GetVisualChild directly"],"exampleFix":"// before\nvar child = bulletDecorator.GetVisualChild(1);\n// after\nif (bulletDecorator.VisualChildrenCount > 1)\n    var child = bulletDecorator.GetVisualChild(1);","handlingStrategy":"validation","validationCode":"if (decorator != null && index >= 0 && index < decorator.VisualChildrenCount) { var c = decorator.GetVisualChild(index); }","typeGuard":"static bool CanGetVisualChild(System.Windows.Media.Visual v, int i) => i >= 0 && i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(v);","tryCatchPattern":"try { var child = decorator.GetVisualChild(index); }\ncatch (ArgumentOutOfRangeException) { /* treat as no child at index */ }","preventionTips":["Always read VisualChildrenCount before GetVisualChild","Remember BulletDecorator's children: optional bullet plus the decorated child","Prefer VisualTreeHelper for traversal"],"tags":["wpf","visual-tree","argument-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}