{"record":{"id":"0ed2d3f5445499ca","repo":"dotnet/wpf","slug":"specified-index-is-out-of-range-or-child-at-index-is-null-do-0ed2d3","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/GridViewRowPresenterBase.cs","lineNumber":125,"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            return _uiElementCollection[index];\n        }\n\n        #endregion\n\n        //-------------------------------------------------------------------\n        //\n        // Internal Methods / Properties\n        //\n        //-------------------------------------------------------------------\n\n        #region Internal Methods\n\n        /// <summary>\n        /// process the column collection chagned event\n        /// </summary>\n        internal virtual void OnColumnCollectionChanged(GridViewColumnCollectionChangedEventArgs e)","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/GridViewRowPresenterBase.cs#L107-L143","documentation":"GridViewRowPresenterBase.GetVisualChild(index) throws ArgumentOutOfRangeException when the presenter has no internal UIElementCollection (no child rows have been created). The API contract forbids calling GetVisualChild when VisualChildrenCount is zero; this implementation additionally indexes the collection directly, so an out-of-range index also throws.","triggerScenarios":"Calling GetVisualChild on a GridViewRowPresenterBase-derived presenter (e.g. GridViewRowPresenter/GridViewHeaderRowPresenter) before its items/columns have created the UIElementCollection, or with an index outside the collection's Count.","commonSituations":"Visual-tree inspection during a layout pass before the ListView's items are realized, or hard-coded child indexes when templating ListView/GridView.","solutions":["Check VisualChildrenCount > 0 before calling GetVisualChild","Defer visual-tree inspection until the presenter is loaded and realized (e.g. Loaded event)","Use VisualTreeHelper.GetChild within the reported count instead of fixed indexes"],"exampleFix":"// before\nvar child = rowPresenter.GetVisualChild(0);\n// after\nif (rowPresenter.VisualChildrenCount > 0)\n    var child = rowPresenter.GetVisualChild(0);","handlingStrategy":"validation","validationCode":"if (presenter.VisualChildrenCount > 0 && index < presenter.VisualChildrenCount) { var c = presenter.GetVisualChild(index); }","typeGuard":"static bool CanGetChild(System.Windows.Controls.Primitives.GridViewRowPresenterBase p, int i) => i >= 0 && i < p.VisualChildrenCount;","tryCatchPattern":"try { var child = presenter.GetVisualChild(index); }\ncatch (ArgumentOutOfRangeException) { /* presenter not realized yet or bad index */ }","preventionTips":["Defer visual-tree inspection until after Loaded/layout","Never assume the presenter has children; check the count","Use VisualTreeHelper.GetChild with the dynamic count"],"tags":["wpf","listview","visual-tree"],"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-21T21:30:21.729Z"}