stanfordnlp/CoreNLP · error · RuntimeException

Error: could not find a constructor for objects of

Error message

Error: could not find a constructor for objects of ${HASH_MAP_CLASS} which takes an existing Map argument.

What it means

Generics.newHashMap(Map) lazily looks up a Constructor<->Map constructor for the configured HASH_MAP_CLASS (a fast/linked map class chosen via system properties). If reflection cannot find a constructor accepting a Map argument, it throws this RuntimeException, meaning the configured map class is incompatible with this factory method.

Solutions

  1. Check the hashmap.class (and similar) system properties; remove them to use the default java.util.HashMap subclass
  2. Ensure the configured class is public, concrete, and has a constructor accepting java.util.Map
  3. Verify the fully-qualified class name has no typos and is on the classpath
  4. Call Generics.newHashMap() with no arguments and putAll(existingMap) instead

Example fix

// before
-Dhashmap.class=com.example.MyCustomMap  // has no Map constructor
Map<K,V> m = Generics.newHashMap(existingMap);
// after
-Dhashmap.class=java.util.HashMap   // or remove the property entirely
Map<K,V> m = Generics.newHashMap(existingMap);
Defensive patterns

Strategy: validation

Validate before calling

Class<?> cls = Class.forName(System.getProperty("hashmap.class", "java.util.HashMap"));
if (!java.util.Map.class.isAssignableFrom(cls)) throw new IllegalStateException("hashmap.class is not a Map");
boolean ok = java.util.Arrays.stream(cls.getConstructors()).anyMatch(c -> c.getParameterCount() == 1 && c.getParameterTypes()[0] == java.util.Map.class);
if (!ok) throw new IllegalStateException(cls + " lacks a Map constructor");

Try / catch

try { Map<K,V> m = Generics.newHashMap(existing); } catch (RuntimeException e) { Map<K,V> m = new HashMap<>(existing); }

Prevention

When it happens

Trigger: Calling Generics.newHashMap(existingMap) when the hashmap.class system property names a class that lacks a public constructor taking a Map (e.g. a class with only a no-arg constructor, an abstract class, or a typo'd class name).

Common situations: Setting -Dhashmap.class to a custom or newer map implementation while upgrading Stanford CoreNLP versions; typos in the fully-qualified class name; the class failing to load so reflection resolves against the wrong type.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/util/Generics.java:194

    }
  }

  // must be called after HASH_MAP_CLASS is defined
  private static Constructor getHashMapSizeConstructor() {
    try {
      return HASH_MAP_CLASS.getConstructor(Integer.TYPE);
    } catch (Exception e) {
      log.info("Warning: could not find a constructor for objects of " + HASH_MAP_CLASS + " which takes an integer argument.  Will use the no argument constructor instead.");
    }
    return null;
  }

  // must be called after HASH_MAP_CLASS is defined
  private static Constructor getHashMapFromMapConstructor() {
    try {
      return HASH_MAP_CLASS.getConstructor(Map.class);
    } catch (Exception e) {
      throw new RuntimeException("Error: could not find a constructor for objects of " + HASH_MAP_CLASS + " which takes an existing Map argument.", e);
    }
  }

  /* Maps */
  public static <K,V> Map<K,V> newHashMap() {
    try {
      return ErasureUtils.uncheckedCast(HASH_MAP_CLASS.newInstance());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  public static <K,V> Map<K,V> newHashMap(int initialCapacity) {
    if (HASH_MAP_SIZE_CONSTRUCTOR == null) {
      return newHashMap();
    }
    try {
      return ErasureUtils.uncheckedCast(HASH_MAP_SIZE_CONSTRUCTOR.newInstance(initialCapacity));

View on GitHub (pinned to 1b7edd19c4)