stanfordnlp/CoreNLP · error · HashableCoreMapException

Attempt to change value of immutable field

Error message

Attempt to change value of immutable field ${key.getSimpleName()}

What it means

HashableCoreMap refuses set() on keys registered as immutable (immutableKeys), because changing them would invalidate the precomputed hash code. It throws HashableCoreMapException with the key's simple name.

Solutions

  1. Use a non-immutable key, or create a new HashableCoreMap with the desired values
  2. Call makeImmutable only after all writes to that key are complete
  3. Copy the needed values into a regular ArrayMap/CoreMap if you need mutability

Example fix

// before
map.makeImmutable(MyKey.class);
map.set(MyKey.class, newValue); // throws
// after
map.set(MyKey.class, newValue); // write first
map.makeImmutable(MyKey.class); // freeze afterwards
Defensive patterns

Strategy: try-catch

Validate before calling

// cannot inspect immutableKeys externally; guard by construction: never set a key after makeImmutable

Try / catch

try { map.set(key, value); } catch (HashableCoreMapException e) { /* rebuild a new map with the desired value */ }

Prevention

When it happens

Trigger: Calling map.set(SomeKey.class, value) where SomeKey was added via makeImmutable(...) earlier — e.g. trying to overwrite a key after the map's hash was frozen.

Common situations: Annotation pipelines that freeze keys (like CoreMap keys used in hash-based caching) and later code attempting to update those same fields; accidentally reusing a frozen map object instead of copying it.

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

Appendix: source

Thrown at src/edu/stanford/nlp/util/HashableCoreMap.java:76

      valueHashcode += super.get((Class)key).hashCode();
    }
    
    this.immutableKeys = hashkey;
    this.hashcode = keyHashcode * 31 + valueHashcode;
  }
  
  /**
   * Sets the value associated with the given key; if the the key is one
   * of the hashable keys, throws an exception.
   * 
   * @throws HashableCoreMapException Attempting to set the value for an
   *   immutable, hashable key.
   */
  @Override
  public <VALUE> VALUE set(Class<? extends Key<VALUE>> key, VALUE value) {
    
    if (immutableKeys.contains(key)) {
      throw new HashableCoreMapException("Attempt to change value " +
      		"of immutable field "+key.getSimpleName());
    }
    
    return super.set(key, value);
  }
  
  /**
   * Provides a hash code based on the immutable keys and values provided
   * to the constructor.
   */
  @Override
  public int hashCode() {
    return hashcode;
  }
  
  /**
   * If the provided object is a HashableCoreMap, equality is based only
   * upon the values of the immutable hashkeys; otherwise, defaults to

View on GitHub (pinned to 1b7edd19c4)