chinabugotech/hutool · warning · UnsupportedOperationException

remove() method not supported for a NodeListIterator.

Error message

remove() method not supported for a NodeListIterator.

What it means

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.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/collection/NodeListIter.java:56

		return nodeList != null && index < nodeList.getLength();
	}

	@Override
	public Node next() {
		if (nodeList != null && index < nodeList.getLength()) {
			return nodeList.item(index++);
		}
		throw new NoSuchElementException("underlying nodeList has no more elements");
	}

	/**
	 * Throws {@link UnsupportedOperationException}.
	 *
	 * @throws UnsupportedOperationException always
	 */
	@Override
	public void remove() {
		throw new UnsupportedOperationException("remove() method not supported for a NodeListIterator.");
	}

	@Override
	public void reset() {
		this.index = 0;
	}
}

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Do not call remove() on NodeListIter — collect results into a new list instead of removing from the iterator.
  2. Use a different iteration approach that builds a filtered collection rather than removing in-place.
  3. Check if the iterator supports removal before calling remove() (though Java's Iterator interface does not expose this).

Example fix

// before
NodeListIter iter = new NodeListIter(nodeList);
while (iter.hasNext()) {
    Node node = iter.next();
    if (shouldRemove(node)) iter.remove(); // throws UnsupportedOperationException
}

// after
List<Node> kept = new ArrayList<>();
NodeListIter iter = new NodeListIter(nodeList);
while (iter.hasNext()) {
    Node node = iter.next();
    if (!shouldRemove(node)) kept.add(node);
}
Defensive patterns

Strategy: validation

Validate before calling

// Check iterator type before calling remove
if (!(iterator instanceof NodeListIter)) {
    iterator.remove();
}

Type guard

public static boolean isRemovableIterator(Iterator<?> iter) {
    return !(iter instanceof NodeListIter);
}

Try / catch

try {
    iter.remove();
} catch (UnsupportedOperationException e) {
    // iterator does not support removal — build filtered list instead
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/7227fccb201b4620. Report an issue: GitHub.