UnsupportedOperationException

java.lang.UnsupportedOperationException

Quick Answer

You are calling a mutating method on an unmodifiable collection. Use a mutable collection like ArrayList instead.

Why This Happens

Collections returned by List.of(), Collections.unmodifiableList(), and Arrays.asList() do not support structural modification. Calling add(), remove(), or set() on them throws this exception. You need to copy the elements into a mutable collection first.

The Problem

List<String> list = List.of("a", "b", "c");
list.add("d"); // UnsupportedOperationException

The Fix

List<String> list = new ArrayList<>(List.of("a", "b", "c"));
list.add("d"); // Works on mutable ArrayList

Step-by-Step Fix

  1. 1

    Identify the unmodifiable collection

    Check how the collection was created. List.of(), Map.of(), Collections.unmodifiable*(), and Arrays.asList() return unmodifiable views.

  2. 2

    Determine if mutation is needed

    Decide whether you actually need to modify the collection or can work with it as-is.

  3. 3

    Copy to a mutable collection

    Wrap the unmodifiable collection in new ArrayList<>(), new HashMap<>(), etc. to get a mutable copy.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free