{
  "id": "java/unsupportedoperationexception-immutable-list",
  "signature": "java.lang.UnsupportedOperationException: null (or with message: ImmutableList does not support modification)",
  "signature_zh": "java.lang.UnsupportedOperationException：null（或带有消息：ImmutableList不支持修改）",
  "regex": "java\\.lang\\.UnsupportedOperationException",
  "domain": "java",
  "category": "runtime_error",
  "subcategory": null,
  "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().",
  "root_cause_type": "generic",
  "root_cause_zh": "当尝试通过调用add()、remove()或set()等方法修改不可变集合（例如List.of()、Collections.unmodifiableList()或Guava的ImmutableList）时发生。",
  "versions": [
    {
      "version": "Java 9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Java 11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Java 17",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Java 21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Guava 31.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Catching the exception and ignoring it",
      "why_fails": "The collection remains unmodified; ignoring the exception does not achieve the intended mutation and may lead to data inconsistencies.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using Arrays.asList() instead of List.of() but still calling add()",
      "why_fails": "Arrays.asList() returns a fixed-size list backed by the array; add() still throws UnsupportedOperationException because the size is fixed.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Casting the list to ArrayList and modifying it",
      "why_fails": "The underlying object is not an ArrayList; casting will throw ClassCastException.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Create a mutable copy of the immutable list before modification: `List<String> mutableList = new ArrayList<>(immutableList); mutableList.add(\"new element\");`",
      "success_rate": 0.95,
      "how": "Create a mutable copy of the immutable list before modification: `List<String> mutableList = new ArrayList<>(immutableList); mutableList.add(\"new element\");`",
      "condition": "",
      "sources": []
    },
    {
      "action": "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.",
      "success_rate": 0.9,
      "how": "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.",
      "condition": "",
      "sources": []
    },
    {
      "action": "For Guava ImmutableList, use the copyOf() method to create a mutable ArrayList: `List<String> mutableList = new ArrayList<>(ImmutableList.copyOf(originalList));`",
      "success_rate": 0.9,
      "how": "For Guava ImmutableList, use the copyOf() method to create a mutable ArrayList: `List<String> mutableList = new ArrayList<>(ImmutableList.copyOf(originalList));`",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Create a mutable copy of the immutable list before modification: `List<String> mutableList = new ArrayList<>(immutableList); mutableList.add(\"new element\");`",
    "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.",
    "For Guava ImmutableList, use the copyOf() method to create a mutable ArrayList: `List<String> mutableList = new ArrayList<>(ImmutableList.copyOf(originalList));`"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/UnsupportedOperationException.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.9,
  "fix_success_rate": 0.95,
  "resolvable": "true",
  "first_seen": "2023-03-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}