{"record":{"id":"7227fccb201b4620","repo":"chinabugotech/hutool","slug":"remove-method-not-supported-for-a-nodelistiterat","errorCode":null,"errorMessage":"remove() method not supported for a NodeListIterator.","messagePattern":"remove\\(\\) method not supported for a NodeListIterator\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"warning","filePath":"hutool-core/src/main/java/cn/hutool/core/collection/NodeListIter.java","lineNumber":56,"sourceCode":"\t\treturn nodeList != null && index < nodeList.getLength();\n\t}\n\n\t@Override\n\tpublic Node next() {\n\t\tif (nodeList != null && index < nodeList.getLength()) {\n\t\t\treturn nodeList.item(index++);\n\t\t}\n\t\tthrow new NoSuchElementException(\"underlying nodeList has no more elements\");\n\t}\n\n\t/**\n\t * Throws {@link UnsupportedOperationException}.\n\t *\n\t * @throws UnsupportedOperationException always\n\t */\n\t@Override\n\tpublic void remove() {\n\t\tthrow new UnsupportedOperationException(\"remove() method not supported for a NodeListIterator.\");\n\t}\n\n\t@Override\n\tpublic void reset() {\n\t\tthis.index = 0;\n\t}\n}\n","sourceCodeStart":38,"sourceCodeEnd":64,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/collection/NodeListIter.java#L38-L64","documentation":"Thrown unconditionally by NodeListIter.remove() — this iterator over org.w3c.dom.NodeList does not support element removal. The DOM NodeList is a read-only view and cannot be structurally modified through its iterator. The method always throws UnsupportedOperationException as designed.","triggerScenarios":"Calling iter.remove() on any NodeListIter instance. This can happen when passing a NodeListIter to code that calls remove() on iterators (e.g., Collection.removeIf on a collection backed by NodeListIter, or manual iterator-based removal loops).","commonSituations":"Generic iterator-processing code that calls remove() as part of a filtering pattern. Refactoring DOM processing code to use iterator patterns that assume mutability. Framework code that invokes remove() on any Iterator it receives.","solutions":["Do not call remove() on NodeListIter — collect results into a new list instead of removing from the iterator.","Use a different iteration approach that builds a filtered collection rather than removing in-place.","Check if the iterator supports removal before calling remove() (though Java's Iterator interface does not expose this)."],"exampleFix":"// before\nNodeListIter iter = new NodeListIter(nodeList);\nwhile (iter.hasNext()) {\n    Node node = iter.next();\n    if (shouldRemove(node)) iter.remove(); // throws UnsupportedOperationException\n}\n\n// after\nList<Node> kept = new ArrayList<>();\nNodeListIter iter = new NodeListIter(nodeList);\nwhile (iter.hasNext()) {\n    Node node = iter.next();\n    if (!shouldRemove(node)) kept.add(node);\n}","handlingStrategy":"validation","validationCode":"// Check iterator type before calling remove\nif (!(iterator instanceof NodeListIter)) {\n    iterator.remove();\n}","typeGuard":"public static boolean isRemovableIterator(Iterator<?> iter) {\n    return !(iter instanceof NodeListIter);\n}","tryCatchPattern":"try {\n    iter.remove();\n} catch (UnsupportedOperationException e) {\n    // iterator does not support removal — build filtered list instead\n}","preventionTips":["Never call remove() on NodeListIter.","Use a collect-into-new-list pattern instead of in-place removal for DOM iteration.","Document read-only iterator behavior in API contracts."],"tags":["collection","iterator","dom","unsupported-operation","node-list"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}