java.lang.UnsupportedOperationException
java.lang.UnsupportedOperationException
DodaTech
3 min read
java.lang.UnsupportedOperationException in Kotlin occurs when you call add() or remove() on a read-only collection created with listOf() or similar.
What It Means
Kotlin distinguishes between read-only collections (List, Set, Map) and mutable collections (MutableList, MutableSet, MutableMap). Read-only collections do not expose modification methods at compile time — but some underlying Java implementations (like Arrays.asList() or listOf()) do have add()/remove() at runtime via JVM interop, and calling them throws UnsupportedOperationException.
Why It Happens
- You used
listOf()and then called.add()on the result —listOf()returns a read-onlyList. - You converted a
ListtoMutableListusing.toMutableList()but still used the original reference. - You called
Arrays.asList()(Java interop) then tried to add or remove elements. - You used
Collections.unmodifiableList()or similar Java wrapper. - You called
.asList()on an array — the returned list is backed by the array and doesn’t support structural modification.
How to Fix It
1. Use mutableListOf() instead of listOf()
// ❌ UnsupportedOperationException
val items = listOf("a", "b", "c")
items.add("d")
// ✅ Use a mutable collection
val items = mutableListOf("a", "b", "c")
items.add("d") // works fine2. Convert a read-only list to mutable when needed
// If you receive a List from an API, convert it:
fun process(input: List<String>) {
// ❌ input.add("new") — UnsupportedOperationException
val mutable = input.toMutableList()
mutable.add("new") // ✅ OK
}3. Declare the correct collection type
// ❌ Declared as List but need add/remove
class ShoppingCart {
val items: List<String> = mutableListOf() // exposed as read-only
fun addItem(item: String) {
items.add(item) // UnsupportedOperationException at runtime
}
}
// ✅ Store as MutableList internally
class ShoppingCart {
private val _items = mutableListOf<String>()
val items: List<String> get() = _items // expose read-only
fun addItem(item: String) {
_items.add(item)
}
}4. Avoid Arrays.asList() for mutable operations
// ❌ Arrays.asList() returns a fixed-size list
val list = Arrays.asList("a", "b", "c")
list.add("d") // UnsupportedOperationException
// ✅ Use mutableListOf or ArrayList directly
val list = mutableListOf("a", "b", "c")
list.add("d")5. Use toMutableList() for defensive copies
class Team {
private val members = mutableListOf("Alice", "Bob")
// Return a mutable copy so callers can modify without affecting internal state
fun getMembers(): MutableList<String> = members.toMutableList()
}Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro