stanfordnlp/CoreNLP · error · IllegalStateException

Not training, or training was already finalized

Error message

Not training, or training was already finalized

What it means

Classifier (nndep) methods that mutate training state (like validateTraining callers: initGradientHistories, preTraining, etc.) require the classifier to be in training mode. The private validateTraining() throws IllegalStateException when isTraining is false, i.e. training never started or finalizeTraining() was already called (which flips training mode off).

Solutions

  1. Ensure training is started (training mode enabled) before calling training methods
  2. Do not call finalizeTraining() until all training iterations are complete; never call it twice
  3. Re-train or construct a new classifier instance if you already finalized training and need to train more

Example fix

// before
classifier.finalizeTraining();
classifier.addSample(data); // throws
// after
classifier.addSample(data); // while still training
classifier.finalizeTraining(); // only once, at the very end
Defensive patterns

Strategy: validation

Validate before calling

// call training APIs only before finalizeTraining()
if (trainingDone) throw new IllegalStateException("Training already finalized");
classifier.addSample(sample);

Try / catch

try {
  classifier.addSample(sample);
} catch (IllegalStateException e) {
  // recreate classifier or restart training
}

Prevention

When it happens

Trigger: Calling training-time methods (e.g. Classifier's constructor path calling initGradientHistories, or addSample/gradient updates) after finalizeTraining() or on a classifier constructed for inference only.

Common situations: Calling finalizeTraining() twice; resuming training on a loaded model without re-entering training mode; invoking training APIs from a script after training loop completed.

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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/nndep/Classifier.java:630

    eg2E = new double[E.length][E[0].length];
    eg2W1 = new double[W1.length][W1[0].length];
    eg2b1 = new double[b1.length];
    eg2W2 = new double[W2.length][W2[0].length];
  }

  /**
   * Clear all gradient histories used for AdaGrad training.
   *
   * @throws java.lang.IllegalStateException If not training
   */
  public void clearGradientHistories() {
    validateTraining();
    initGradientHistories();
  }

  private void validateTraining() {
    if (!isTraining)
      throw new IllegalStateException("Not training, or training was already finalized");
  }

  /**
   * Finish training this classifier; prepare for a shutdown.
   */
  public void finalizeTraining() {
    validateTraining();

    // Destroy threadpool
    jobHandler.join(true);

    isTraining = false;
  }

  /**
   * @see #preCompute(java.util.Set)
   */
  public void preCompute() {

View on GitHub (pinned to 1b7edd19c4)