stanfordnlp/CoreNLP · error · RuntimeException

attempt to get word when sentence and lattice are null!

Error message

attempt to get word when sentence and lattice are null!

What it means

During lattice-based parse reconstruction, ExhaustivePCFGParser expects either the sentence tokens or a word lattice to be available. If both are null when it tries to fetch a word for a tag node, it throws this RuntimeException, signaling an uninitialized parse state.

Solutions

  1. Always check parserQuery.parse(...) returned true before calling getBestParse().
  2. Ensure the sentence or lattice was supplied to the parser before reconstruction.
  3. Initialize the parser query through the normal parse pipeline rather than manipulating internal fields.

Example fix

// before
parserQuery.getBestParse(); // parse never called or failed
// after
if (parserQuery.parse(sentence)) {
  Tree t = parserQuery.getBestParse();
}
Defensive patterns

Strategy: validation

Validate before calling

if (!pq.parse(sentence)) { throw new IllegalStateException("parse failed; no reconstruction possible"); }
Tree t = pq.getBestParse();

Try / catch

try { t = pq.getBestParse(); } catch (RuntimeException e) { if (e.getMessage().contains("sentence and lattice are null")) { /* parse was never run or failed */ } else throw e; }

Prevention

When it happens

Trigger: Requesting getBestParse() (reconstruction) when the query was not populated via parse(...) with a sentence or lattice, or internal state (sentence/lattice fields) was never set, e.g. calling getBestParse() without a prior successful parse call.

Common situations: Calling getBestParse() before calling parse(); a parse failed (returned false) but reconstruction was still attempted; programmatic use constructing the ParserQuery without proper input.

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/9d603e648bfcf08d. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/parser/lexparser/ExhaustivePCFGParser.java:1594

            IntTaggedWord itw = new IntTaggedWord(edge.word, stateIndex.get(goal), wordIndex, tagIndex);

            float tagScore = (floodTags) ? -1000.0f : lex.score(itw, start, edge.word, null);
            if (matches(bestScore, tagScore + (float) edge.weight)) {
              wordNode = tf.newLeaf(edge.word);
              if(wordNode.label() instanceof CoreLabel) {
              	CoreLabel cl = (CoreLabel) wordNode.label();
              	cl.setBeginPosition(start);
              	cl.setEndPosition(end);
              }
              break;
            }
          }
          if (wordNode == null) {
            throw new RuntimeException("could not find matching word from lattice in parse reconstruction");
          }

        } else {
          throw new RuntimeException("attempt to get word when sentence and lattice are null!");
        }
        Tree tagNode = tf.newTreeNode(goalStr, Collections.singletonList(wordNode));
        tagNode.setScore(bestScore);
        if (originalTags[start] != null) {
          tagNode.label().setValue(originalTags[start].tag());
        }
        return tagNode;
      } else {  // normal lexicon is single words case
        IntTaggedWord tagging = new IntTaggedWord(words[start], tagIndex.indexOf(goalStr));
        String contextStr = getCoreLabel(start).originalText();
        float tagScore = lex.score(tagging, start, wordIndex.get(words[start]), contextStr);
        if (tagScore > Float.NEGATIVE_INFINITY || floodTags) {
          // return a pre-terminal tree
          CoreLabel terminalLabel = getCoreLabel(start);

          Tree wordNode = tf.newLeaf(terminalLabel);
          Tree tagNode = tf.newTreeNode(goalStr, Collections.singletonList(wordNode));
          tagNode.setScore(bestScore);

View on GitHub (pinned to 1b7edd19c4)