stanfordnlp/CoreNLP · error · java.lang.UnsupportedOperationException

toArray is not supported for…

Error message

toArray is not supported for PhraseTable.PhraseStringCollection

What it means

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.

Solutions

  1. Build the array yourself from the table iterator: loop over Phrase and call getText().
  2. Copy into a List<String> first, then call toArray on that list.
  3. Use CollectionUtils or a manual loop instead of passing this collection to toArray-based APIs.
  4. Subclass/patch PhraseStringCollection to implement toArray if you own the code.

Example fix

// before
Object[] arr = table.getStringCollection().toArray(); // throws
// after
List<String> list = new ArrayList<>();
for (Phrase p : table) { list.add(p.getText()); }
String[] arr = list.toArray(new String[0]);
Defensive patterns

Strategy: fallback

Validate before calling

Object[] arr;
try { arr = coll.toArray(); } catch (UnsupportedOperationException e) { arr = null; }

Try / catch

try { arr = coll.toArray(); } catch (UnsupportedOperationException e) { List<String> list = new ArrayList<>(); for (Phrase p : table) list.add(p.getText()); arr = list.toArray(); }

Prevention

When it happens

Trigger: Calling toArray() on the collection returned by PhraseTable.getStringCollection(), directly or via APIs that copy collections into arrays (e.g. Object[] a = coll.toArray()).

Common situations: Passing the string collection to utility methods that call toArray internally; logging/frameworks that serialize collections as arrays.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/a989975a712e4ec9. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java:1147

          return (phraseTable.lookup((String) o) != null);
        }
      } else {
        return false;
      }
    }

    public Iterator<String> iterator() {
      throw new UnsupportedOperationException("iterator is not supported for PhraseTable.PhraseStringCollection");
//      return new FunctionApplyingIterator( phraseTable.iterator(), new Function<Phrase,String>() {
//        @Override
//        public String apply(Phrase in) {
//          return in.getText();
//        }
//      });
    }

    public Object[] toArray() {
      throw new UnsupportedOperationException("toArray is not supported for PhraseTable.PhraseStringCollection");
    }

    public <T> T[] toArray(T[] a) {
      throw new UnsupportedOperationException("toArray is not supported for PhraseTable.PhraseStringCollection");
    }

    public boolean add(String s) {
      return phraseTable.addPhrase(s);
    }

    public boolean remove(Object o) {
      throw new UnsupportedOperationException("Remove is not supported for PhraseTable.PhraseStringCollection");
    }

    public boolean containsAll(Collection<?> c) {
      for (Object o:c) {
        if (!contains(o)) {
          return false;

View on GitHub (pinned to 1b7edd19c4)