{"record":{"id":"a989975a712e4ec9","repo":"stanfordnlp/CoreNLP","slug":"toarray-is-not-supported-for-phrasetable-phrasestr","errorCode":null,"errorMessage":"toArray is not supported for PhraseTable.PhraseStringCollection","messagePattern":"toArray is not supported for PhraseTable\\.PhraseStringCollection","errorType":"exception","errorClass":"java.lang.UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java","lineNumber":1147,"sourceCode":"          return (phraseTable.lookup((String) o) != null);\n        }\n      } else {\n        return false;\n      }\n    }\n\n    public Iterator<String> iterator() {\n      throw new UnsupportedOperationException(\"iterator is not supported for PhraseTable.PhraseStringCollection\");\n//      return new FunctionApplyingIterator( phraseTable.iterator(), new Function<Phrase,String>() {\n//        @Override\n//        public String apply(Phrase in) {\n//          return in.getText();\n//        }\n//      });\n    }\n\n    public Object[] toArray() {\n      throw new UnsupportedOperationException(\"toArray is not supported for PhraseTable.PhraseStringCollection\");\n    }\n\n    public <T> T[] toArray(T[] a) {\n      throw new UnsupportedOperationException(\"toArray is not supported for PhraseTable.PhraseStringCollection\");\n    }\n\n    public boolean add(String s) {\n      return phraseTable.addPhrase(s);\n    }\n\n    public boolean remove(Object o) {\n      throw new UnsupportedOperationException(\"Remove is not supported for PhraseTable.PhraseStringCollection\");\n    }\n\n    public boolean containsAll(Collection<?> c) {\n      for (Object o:c) {\n        if (!contains(o)) {\n          return false;","sourceCodeStart":1129,"sourceCodeEnd":1165,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java#L1129-L1165","documentation":"PhraseTable.PhraseStringCollection implements Collection<String> but only supports a few operations; toArray() intentionally throws UnsupportedOperationException because converting the phrase table to an array of strings is not implemented.","triggerScenarios":"Calling toArray() on the collection returned by PhraseTable.getStringCollection(), directly or via APIs that copy collections into arrays (e.g. Object[] a = coll.toArray()).","commonSituations":"Passing the string collection to utility methods that call toArray internally; logging/frameworks that serialize collections as arrays.","solutions":["Build the array yourself from the table iterator: loop over Phrase and call getText().","Copy into a List<String> first, then call toArray on that list.","Use CollectionUtils or a manual loop instead of passing this collection to toArray-based APIs.","Subclass/patch PhraseStringCollection to implement toArray if you own the code."],"exampleFix":"// before\nObject[] arr = table.getStringCollection().toArray(); // throws\n// after\nList<String> list = new ArrayList<>();\nfor (Phrase p : table) { list.add(p.getText()); }\nString[] arr = list.toArray(new String[0]);","handlingStrategy":"fallback","validationCode":"Object[] arr;\ntry { arr = coll.toArray(); } catch (UnsupportedOperationException e) { arr = null; }","typeGuard":null,"tryCatchPattern":"try { arr = coll.toArray(); } catch (UnsupportedOperationException e) { List<String> list = new ArrayList<>(); for (Phrase p : table) list.add(p.getText()); arr = list.toArray(); }","preventionTips":["Never call toArray on PhraseStringCollection.","Materialize strings into your own list first.","Use PhraseTable's iterator for enumeration."],"tags":["unsupported-operation","collection","tokensregex"],"backgroundTag":"unsupported-operation","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"}