# java.lang.UnsupportedOperationException: null (or with message: ImmutableList does not support modification)

- **ID:** `java/unsupportedoperationexception-immutable-list`
- **Domain:** java
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

This error occurs when an attempt is made to modify an immutable collection (e.g., List.of(), Collections.unmodifiableList(), or Guava's ImmutableList) by calling methods like add(), remove(), or set().

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Java 9 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |
| Guava 31.x | active | — | — |

## Workarounds

1. **Create a mutable copy of the immutable list before modification: `List<String> mutableList = new ArrayList<>(immutableList); mutableList.add("new element");`** (95% success)
   ```
   Create a mutable copy of the immutable list before modification: `List<String> mutableList = new ArrayList<>(immutableList); mutableList.add("new element");`
   ```
2. **Use the builder pattern for mutable collections from the start: `List<String> list = new ArrayList<>(List.of("a", "b"));` or use `Stream.collect(Collectors.toList())` which returns a mutable list.** (90% success)
   ```
   Use the builder pattern for mutable collections from the start: `List<String> list = new ArrayList<>(List.of("a", "b"));` or use `Stream.collect(Collectors.toList())` which returns a mutable list.
   ```
3. **For Guava ImmutableList, use the copyOf() method to create a mutable ArrayList: `List<String> mutableList = new ArrayList<>(ImmutableList.copyOf(originalList));`** (90% success)
   ```
   For Guava ImmutableList, use the copyOf() method to create a mutable ArrayList: `List<String> mutableList = new ArrayList<>(ImmutableList.copyOf(originalList));`
   ```

## Dead Ends

- **Catching the exception and ignoring it** — The collection remains unmodified; ignoring the exception does not achieve the intended mutation and may lead to data inconsistencies. (80% fail)
- **Using Arrays.asList() instead of List.of() but still calling add()** — Arrays.asList() returns a fixed-size list backed by the array; add() still throws UnsupportedOperationException because the size is fixed. (70% fail)
- **Casting the list to ArrayList and modifying it** — The underlying object is not an ArrayList; casting will throw ClassCastException. (90% fail)
