languagetool-org/languagetool · error · IllegalStateException

enableMapping not activated

Error message

enableMapping not activated

What it means

TextConverter optionally records a position mapping from converted text back to original text. getMapping() throws IllegalStateException when this feature was not enabled before conversion, since the mapping would be empty/meaningless.

Source

Thrown at languagetool-wikipedia/src/main/java/org/languagetool/dev/wikipedia/TextConverter.java:104

  private boolean enableMapping = true;

  // =========================================================================

//  public TextConverter(SimpleWikiConfiguration config, int wrapCol) {
//    this.config = config;
//    this.wrapCol = wrapCol;
//  }

  public void enableMapping(boolean enableMapping) {
    this.enableMapping = enableMapping;
  }

  /**
   * Return a mapping from converted text positions to original text positions.
   */
  public Map<Integer, Location> getMapping() {
    if (!enableMapping) {
      throw new IllegalStateException("enableMapping not activated");
    }
    return mapping;
  }

//  @Override
//  protected boolean before(AstNode node) {
//    // This method is called by go() before visitation starts
//    sb = new StringBuilder();
//    line = new StringBuilder();
//    mapping = new HashMap<>();
//    pastBod = false;
//    needNewlines = 0;
//    needSpace = false;
//    noWrap = false;
//    sections = new LinkedList<>();
//    return super.before(node);
//  }

View on GitHub (pinned to 2e990059ce)

Solutions

  1. Enable mapping on the converter before converting text
  2. Create a new converter with mapping enabled if the text was already converted
  3. Check the converter configuration before calling getMapping()

Example fix

// before
TextConverter conv = new TextConverter();
conv.convert(text);
conv.getMapping(); // IllegalStateException
// after
TextConverter conv = new TextConverter();
conv.setEnableMapping(true);
conv.convert(text);
conv.getMapping();
Defensive patterns

Strategy: try-catch

Validate before calling

if (!converter.isEnableMapping()) { converter.setEnableMapping(true); }

Try / catch

try {
  mapping = converter.getMapping();
} catch (IllegalStateException e) {
  converter.setEnableMapping(true);
  converted = converter.convert(originalText);
  mapping = converter.getMapping();
}

Prevention

When it happens

Trigger: Calling getMapping() on a TextConverter created without enabling mapping (enableMapping flag false) — i.e., converting text but never activating position tracking.

Common situations: Forgetting to call the enable-mapping setter before convert(); reusing a converter instance configured for plain conversion when position mapping is later needed.

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 languagetool-org/languagetool@2e990059ce (2026-09-06). Data as JSON: /api/errors/eeaabf5b5a220af1. Report an issue: GitHub.