{"record":{"id":"4d0533be7bb5eae8","repo":"dotnet/wpf","slug":"specified-index-is-out-of-range-or-child-at-index-is-null-do","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/Panel.cs","lineNumber":375,"sourceCode":"                if (_uiElementCollection == null)\n                {\n                    return 0;\n                }\n                else\n                {\n                    return _uiElementCollection.Count;\n                }\n            }\n        }\n\n        /// <summary>\n        /// Gets the Visual child at the specified index.\n        /// </summary>\n        protected override Visual GetVisualChild(int index)\n        {\n            if (_uiElementCollection == null)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);\n            }\n\n            if (IsZStateDirty) { RecomputeZState(); }\n            int visualIndex = _zLut != null ? _zLut[index] : index;\n            return _uiElementCollection[visualIndex];\n        }\n\n        /// <summary>\n        /// Creates a new UIElementCollection. Panel-derived class can create its own version of\n        /// UIElementCollection -derived class to add cached information to every child or to\n        /// intercept any Add/Remove actions (for example, for incremental layout update)\n        /// </summary>\n        protected virtual UIElementCollection CreateUIElementCollection(FrameworkElement logicalParent)\n        {\n            return new UIElementCollection(this, logicalParent);\n        }\n\n        /// <summary>","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Panel.cs#L357-L393","documentation":"Panel.GetVisualChild throws ArgumentOutOfRangeException when the panel's _uiElementCollection is null (panel has no children) or the index is outside the actual child count. The Visual contract requires callers to honor VisualChildrenCount; requesting a child from an empty panel violates it.","triggerScenarios":"Calling GetVisualChild(index) where index >= VisualChildrenCount or when the panel has no UIElementCollection; custom rendering/subclass code iterating children using a stale count; tooling/profiling code walking the visual tree with wrong bounds.","commonSituations":"Custom Panel subclasses overriding rendering and probing children directly; automation and UI-testing frameworks walking visuals during collection mutation; off-by-one loops (index <= Count instead of <).","solutions":["Check VisualChildrenCount before calling GetVisualChild and iterate with index < count","Use VisualTreeHelper.GetChild which respects the count contract instead of raw GetVisualChild","Re-read the child count after any collection mutation rather than caching it","Treat an empty panel (count 0) as having no children to enumerate"],"exampleFix":"// before\nfor (int i = 0; i <= panel.VisualChildrenCount; i++)\n    var child = panel.GetVisualChild(i); // throws\n// after\nfor (int i = 0; i < panel.VisualChildrenCount; i++)\n    var child = VisualTreeHelper.GetChild(panel, i);","handlingStrategy":"validation","validationCode":"int count = panel.VisualChildrenCount;\nfor (int i = 0; i < count; i++)\n{\n    var child = VisualTreeHelper.GetChild(panel, i);\n}","typeGuard":"Visual GetChildSafe(Visual v, int i) =>\n    (v is Panel p && i >= 0 && i < p.VisualChildrenCount) ? VisualTreeHelper.GetChild(v, i) : null;","tryCatchPattern":"try { var child = panel.GetVisualChild(index); }\ncatch (ArgumentOutOfRangeException) { /* panel empty or stale index; re-read VisualChildrenCount */ }","preventionTips":["Always bound loops by VisualChildrenCount with strict < comparison","Re-read child counts after any add/remove on the panel","Prefer VisualTreeHelper.GetChild over calling GetVisualChild directly","Handle count == 0 as a normal case, not an error path"],"tags":["wpf","panel","visual-tree"],"backgroundTag":"argument-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"}