UnsupportedOperationException
JavaERRORCommonCollection

Operation not supported on this collection

Quick Answer

Wrap unmodifiable lists in new ArrayList<>() before mutating.

What this means

Thrown when a mutating method is called on an unmodifiable collection — most commonly from List.of() or Collections.unmodifiableList().

Why it happens
  1. 1Calling add/remove on List.of() result
  2. 2Arrays.asList() result does not support add/remove

Fix

Copy into mutable collection

Copy into mutable collection
List<String> mutable = new ArrayList<>(List.of("a", "b"));
mutable.add("c"); // works

Why this works

ArrayList constructor copies the immutable list into a mutable backing array.

Code examples
Triggerjava
List.of("a", "b").add("c"); // UnsupportedOperationException
Mutable copyjava
List<String> mutable = new ArrayList<>(immutable);
mutable.add("c");
Arrays.asList size-fixedjava
String[] arr = {"a", "b"};
List<String> list = Arrays.asList(arr);
list.set(0, "x"); // ok — fixed size but mutable elements
list.add("c"); // UnsupportedOperationException
Same error in other languages
Sources
Official documentation ↗

Java SE Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Java errors