{"record":{"id":"9d8cacc68e6e3192","repo":"dotnet/wpf","slug":"specified-index-is-out-of-range-or-child-at-index-is-null-do-9d8cac","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/DocumentPageView.cs","lineNumber":472,"sourceCode":"                    // Size for the page host needs to be set to finalSize\n\n                    // Use previous offset and transform\n                    _pageHost.Arrange(new Rect(_pageHost.CachedOffset, finalSize));\n                }\n            }\n\n            return base.ArrangeOverride(finalSize);\n        }\n\n        /// <summary>\n        /// Derived class must implement to support Visual children. The method must return\n        /// the child at the specified index. Index must be between 0 and GetVisualChildrenCount-1.\n        /// </summary>\n        protected override Visual GetVisualChild(int index)\n        {\n            if (index != 0 || _pageHost == null)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);\n            }\n            return _pageHost;\n        }\n\n        /// <summary>\n        /// Dispose the object.\n        /// </summary>\n        protected void Dispose()\n        {\n            if (!_disposed)\n            {\n                _disposed = true;\n\n                // Cleanup all state associated with Paginator.\n                if (_documentPaginator != null)\n                {\n                    _documentPaginator.GetPageCompleted -= new GetPageCompletedEventHandler(HandleGetPageCompleted);\n                    _documentPaginator.PagesChanged -= new PagesChangedEventHandler(HandlePagesChanged);","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DocumentPageView.cs#L454-L490","documentation":"DocumentPageView.GetVisualChild(index) only supports index 0 and only when the internal _pageHost exists; any other index (or a disposed/null page host) throws ArgumentOutOfRangeException. DocumentPageView hosts exactly one visual child (the DocumentPageHost).","triggerScenarios":"Calling GetVisualChild with an index other than 0, or calling it after Dispose() has cleared _pageHost (when VisualChildrenCount is zero).","commonSituations":"Visual tree walking over FlowDocumentPageViewer/DocumentPageView content, or touching views after the viewer has been disposed or the document unloaded.","solutions":["Only request index 0 and verify VisualChildrenCount > 0 first","Avoid calling GetVisualChild on a disposed DocumentPageView; re-check its lifetime","Use VisualTreeHelper.GetChild within the valid count"],"exampleFix":"// before\nvar host = pageView.GetVisualChild(0); // after Dispose -> throws\n// after\nif (pageView.VisualChildrenCount > 0)\n    var host = pageView.GetVisualChild(0);","handlingStrategy":"validation","validationCode":"if (!pageView.IsDisposed && pageView.VisualChildrenCount > 0) { var host = pageView.GetVisualChild(0); }","typeGuard":"static bool HasPageHost(DocumentPageView pv) => !pv.IsDisposed && pv.VisualChildrenCount > 0;","tryCatchPattern":"try { var host = pageView.GetVisualChild(0); }\ncatch (ArgumentOutOfRangeException) { /* view disposed or has no page host */ }","preventionTips":["Only request index 0 on DocumentPageView","Check lifetime/disposal before inspecting the visual tree","Inspect visuals only after the document page is realized"],"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"}