{"record":{"id":"8dbafeecf6104963","repo":"stanfordnlp/CoreNLP","slug":"unexpected-class-in-list-while-converting-list-to","errorCode":null,"errorMessage":"Unexpected class in list  while converting list to map","messagePattern":"Unexpected class in list  while converting list to map","errorType":"exception","errorClass":"java.lang.RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java","lineNumber":436,"sourceCode":"            throw new RuntimeException(\"Unexpected class in list \" + obj.getClass() + \" while adding word \"\n                    + i + \"(\" + word + \") in phrase \" + phraseText);\n          }\n        }\n        if (!phraseAdded && nMaps == 0) {\n          // add to list\n          Phrase newphrase = new Phrase(wordList, phraseText, tag, phraseData);\n          lookupList.add(newphrase);\n          newPhraseAdded = true;\n          phraseAdded = true;\n          if (lookupList.size() > MAX_LIST_SIZE) {\n            // convert lookupList (should consist only of phrases) to map\n            Map newMap = new HashMap<String,Object>(lookupList.size());\n            for (Object obj:lookupList) {\n              if (obj instanceof Phrase) {\n                Phrase oldphrase = (Phrase) obj;\n                addPhrase(newMap, oldphrase, i+1);\n              } else  {\n                throw new RuntimeException(\"Unexpected class in list \" + obj.getClass() + \" while converting list to map\");\n              }\n            }\n            tree.put(word,newMap);\n          }\n        }\n      } else {\n        throw new RuntimeException(\"Unexpected class in list \" + node.getClass() + \" while adding word \"\n                + i + \"(\" + word + \") in phrase \" + phraseText);\n      }\n      if (phraseAdded) {\n        break;\n      }\n    }\n    if (!phraseAdded) {\n      if (wordList.size() == 0) {\n        log.warn(phraseText + \" not added\");\n      } else {\n        Phrase oldphrase = (Phrase) tree.get(PHRASE_END);","sourceCodeStart":418,"sourceCodeEnd":454,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java#L418-L454","documentation":"When converting a List trie node into a Map (to add a new branching word), addPhrase re-inserts every existing element; any element that is not a Phrase aborts with 'Unexpected class in list ... while converting list to map'.","triggerScenarios":"A list node being promoted to a map contains a non-Phrase element (e.g. the single continuation Map or a foreign object), which cannot be re-keyed under the new word.","commonSituations":"Same corruption sources as 416–418: direct manipulation of the internal tree, deserialization that lost types, or threaded writes without synchronization.","solutions":["Rebuild the phrase table using only public addPhrase/readPhrases entry points","Locate and remove non-Phrase elements from list nodes before inserting longer phrases","Guard population code to run single-threaded or under synchronization"],"exampleFix":"// before\ntreeList.add(someMap); // will break later list->map conversion\n// after\nPhrase p = new Phrase(wordList, text, tag, data);\ntable.addPhrase(...); // library decides list vs map layout","handlingStrategy":"try-catch","validationCode":"// ensure list nodes contain only Phrase before triggering list->map conversion\nif (listNodeContainsNonPhrase) rebuildTable();","typeGuard":"static boolean convertibleList(List<?> l) { return l.stream().allMatch(o -> o instanceof Phrase); }","tryCatchPattern":"try { table.addPhrase(wordList, text, tag, data); } catch (RuntimeException e) { if (e.getMessage().contains(\"converting list to map\")) { rebuildPhraseTableFromSource(); } else throw e; }","preventionTips":["Only add Phrase objects to list nodes","Build the table before concurrent reads/writes","Treat structural exceptions as signals to rebuild from source data"],"tags":["tokensregex","internal-state","trie"],"backgroundTag":"internal-invariant-violation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}