{"record":{"id":"a23f5a89813c6415","repo":"dotnet/wpf","slug":"elementnotavailableexception-windowslistview","errorCode":null,"errorMessage":"ElementNotAvailableException","messagePattern":"ElementNotAvailableException","errorType":"exception","errorClass":"ElementNotAvailableException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListView.cs","lineNumber":208,"sourceCode":"        #endregion ProxySimple Interface\n\n        #region ProxyFragment Interface\n\n        internal override ProxySimple GetNextSibling (ProxySimple child)\n        {\n            bool hasGroup = IsGroupViewEnabled (_hwnd);\n            int item = child._item;\n            int count = -1;\n\n            if (!hasGroup)\n            {\n                // Determine how many items are in the list view.\n                count = GetItemCount (_hwnd);\n\n                // Next for an item that does not exist in the list\n                if (item >= count)\n                {\n                    throw new ElementNotAvailableException ();\n                }\n\n                // Check if index of the next item is in range\n                if (item >= 0 && (item + 1) < count)\n                {\n                    // return a node to represent the requested item.\n                    return CreateListViewItem (item + 1);\n                }\n            }\n\n            if (hasGroup)\n            {\n                if (child is ListViewItem)\n                {\n                    throw new InvalidOperationException(SR.OperationCannotBePerformed);\n                }\n\n                WindowsListViewGroup windowsListViewGroup = child as WindowsListViewGroup;","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListView.cs#L190-L226","documentation":"WindowsListView.GetNextSibling throws ElementNotAvailableException when the requested item index is greater than or equal to the current list-view item count. The element that previously existed in the tree is gone — the provider refuses to hand out a node for a non-existent item. This is the standard UIA signal for a stale element.","triggerScenarios":"RaiseEventsOnClient walks the sibling chain and asks for the item after 'item', but items were deleted from the Win32 list-view (e.g. LVM_DELETEITEM) so item >= GetItemCount(_hwnd).","commonSituations":"List contents refreshed/cleared by the app while a UIA event handler is still traversing old element references; bulk list updates firing events mid-mutation.","solutions":["Re-find the element from the automation root instead of using the stale sibling reference.","In event handlers, catch ElementNotAvailableException and ignore events for elements that no longer exist.","Pause list updates (or batch them) before automation traverses the tree.","Re-query the item count and adjust traversal logic to the new count."],"exampleFix":"// before\nvar next = AutomationElement.RootElement.FindFirst(TreeScope.Children, cond); // walks stale chain\n// after\ntry { /* traversal step */ }\ncatch (ElementNotAvailableException) { return; /* element was removed */ }","handlingStrategy":"try-catch","validationCode":"if (itemIndex >= listViewElement.FindAll(TreeScope.Children, Condition.TrueCondition).Count) { /* stale ref; re-find */ }","typeGuard":null,"tryCatchPattern":"try { ProcessSibling(element); } catch (ElementNotAvailableException) { /* element removed; abandon traversal */ }","preventionTips":["Catch ElementNotAvailableException in all UIA event handlers","Re-find elements from the root after list updates","Avoid traversing trees while the app is bulk-updating lists"],"tags":["ui-automation","stale-element","listview"],"backgroundTag":"entity-not-found","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"}