stanfordnlp/CoreNLP · error · IllegalStateException

getPCFGScore called before a sentence has been parsed

Error message

getPCFGScore called before a sentence has been parsed

What it means

ExternalParserQuery.getPCFGScore returns the score of the first stored parse result, but only after a parse has run; if 'results' is still null it throws IllegalStateException('getPCFGScore called before a sentence has been parsed'). It enforces the API contract: parse before querying scores.

Solutions

  1. Call parse(sentence) (or parseAndScore) on the ParserQuery before reading getPCFGScore().
  2. Guard the call: track whether a parse was performed for this query instance.
  3. Wrap in try-catch for IllegalStateException and return a sentinel/throw a clearer domain error.
  4. Create a new ParserQuery per sentence instead of sharing/querying stale instances.

Example fix

// before
double score = parserQuery.getPCFGScore();
// after
if (parserQuery.parse(sentence)) {
  double score = parserQuery.getPCFGScore();
}
Defensive patterns

Strategy: validation

Validate before calling

boolean parsed = parserQuery.parse(sentence);
if (!parsed) throw new IllegalStateException("parse() must be called (and succeed) before getPCFGScore()");

Try / catch

try {
  double s = query.getPCFGScore();
} catch (IllegalStateException e) {
  // query never parsed; run parse first or return sentinel
}

Prevention

When it happens

Trigger: Calling getPCFGScore() on a freshly created ExternalParserQuery (or after a query on which parse() was never invoked) — commonly via getBestScore() which delegates to it.

Common situations: Code that inspects query results without checking whether parse() was called or succeeded, lifecycle bugs where the query object is queried before use, or exception paths that skipped the parse step.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/metrics/ExternalParserQuery.java:45

    this.originalSentence = sentence;
    this.results = results;
    this.success = (results != null);
  }

  @Override
  public boolean parse(List<? extends HasWord> sentence) {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean parseAndReport(List<? extends HasWord> sentence, PrintWriter pwErr) {
    return parse(sentence);
  }

  @Override
  public double getPCFGScore() {
    if (results == null) {
      throw new IllegalStateException("getPCFGScore called before a sentence has been parsed");
    }
    return results.get(0).score();
  }

  @Override
  public Tree getBestParse() {
    if (results == null) {
      throw new IllegalStateException("getPCFGScore called before a sentence has been parsed");
    }
    return results.get(0).object();
  }

  @Override
  public List<ScoredObject<Tree>> getKBestParses(int k) {
    if (results == null) {
      throw new IllegalStateException("getPCFGScore called before a sentence has been parsed");
    }
    if (results.size() > k) {

View on GitHub (pinned to 1b7edd19c4)