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_SET_CLASS} which takes an existing collection argument.

What it means

Generics.getHashSetCollectionConstructor reflectively looks up a HashSet constructor accepting a Collection (the configured HASH_SET_CLASS) and throws RuntimeException wrapping the reflective failure if none exists. This occurs at static initialization time when building the HASH_SET_COLLECTION_CONSTRUCTOR holder.

Solutions

  1. Ensure HASH_SET_CLASS (default java.util.HashSet) exposes a public constructor taking Collection; revert to HashSet if customized.
  2. Add the missing constructor to the custom set class: public MySet(Collection<? extends E> c).
  3. Check for classpath conflicts pulling in an unexpected HASH_SET_CLASS implementation.
  4. Inspect the wrapped cause (e.getCause()) to confirm whether it is NoSuchMethodException vs. a security/ClassNotFound problem and fix accordingly.

Example fix

// before
public class MySet<E> extends AbstractSet<E> { public MySet() {...} } // no Collection ctor
// after
public class MySet<E> extends AbstractSet<E> {
  public MySet() {...}
  public MySet(Collection<? extends E> c) { addAll(c); }
}
Defensive patterns

Strategy: try-catch

Validate before calling

try { HASH_SET_CLASS.getConstructor(Collection.class); } catch (NoSuchMethodException e) { /* configured set class lacks Collection ctor */ }

Type guard

boolean hasCollectionConstructor(Class<?> cls) { try { cls.getConstructor(Collection.class); return true; } catch (Exception e) { return false; } }

Try / catch

try { Set<E> s = Generics.newHashSet(existing); } catch (RuntimeException e) { Set<E> s = new HashSet<>(existing); }

Prevention

When it happens

Trigger: Static initialization of Generics when HASH_SET_CLASS does not declare a public Constructor(Collection) — e.g. a custom set class substituted via configuration that lacks the Collection-arg constructor, or NoSuchMethodException/SecurityException during reflection.

Common situations: Swapping in a non-standard Set implementation whose constructor signatures differ; unusual JDK/security-manager environments blocking reflection; classpath changes replacing the expected class.

Related errors


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

Appendix: source

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

    }
  }

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

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

  public static <E> Set<E> newHashSet() {
    try {
      return ErasureUtils.uncheckedCast(HASH_SET_CLASS.newInstance());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  public static <E> Set<E> newHashSet(int initialCapacity) {
    if (HASH_SET_SIZE_CONSTRUCTOR == null) {
      return newHashSet();
    }
    try {
      return ErasureUtils.uncheckedCast(HASH_SET_SIZE_CONSTRUCTOR.newInstance(initialCapacity));
    } catch (Exception e) {

View on GitHub (pinned to 1b7edd19c4)