stanfordnlp/CoreNLP · error · RuntimeException

dim should be an array of size 2.

Error message

dim should be an array of size 2.

What it means

ErasureUtils.mkT2DArray creates a generic two-dimensional array via reflection and throws RuntimeException when the dim array does not have exactly two elements, since a 2D array requires exactly two dimension lengths. This is an internal argument-shape check.

Solutions

  1. Pass exactly two dimensions, e.g. new int[]{rows, cols}.
  2. Fix the code computing dim to produce two entries for 2D arrays.
  3. Add a validation/assertion where dim is built so bad shapes fail at the source.
  4. Use mkTArray (1D) or a different utility if the intended rank differs.

Example fix

// before
String[][] arr = ErasureUtils.mkT2DArray(String.class, new int[]{10});
// after
String[][] arr = ErasureUtils.mkT2DArray(String.class, new int[]{10, 20});
Defensive patterns

Strategy: validation

Validate before calling

if (dim == null || dim.length != 2) throw new IllegalArgumentException("dim must have exactly 2 entries, got " + (dim == null ? "null" : dim.length));

Type guard

boolean is2DDim(int[] dim) { return dim != null && dim.length == 2; }

Try / catch

try { T[][] arr = ErasureUtils.mkT2DArray(klass, dim); } catch (RuntimeException e) { /* fix dim shape or fall back to 1D utility */ }

Prevention

When it happens

Trigger: Calling mkT2DArray(klass, dim) with an int[] whose length is 1, 3, or 0 — e.g. passing a single dimension from 1D array-creation code or reusing a dims array meant for another utility.

Common situations: Generic array-builder code shared between 1D and 2D cases passing the wrong dims; refactors changing array rank without updating dim construction.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/util/ErasureUtils.java:50

  /**
   * Makes an array based on klass, but casts it to be of type T[]. This is a very
   * unsafe operation and should be used carefully. Namely, you should ensure that
   * klass is a subtype of T, or that klass is a supertype of T *and* that the array
   * will not escape the generic constant *and* that klass is the same as the erasure
   * of T.
   * @param <T>
   */
  @SuppressWarnings("unchecked")
  public static <T> T[] mkTArray(Class<?> klass, int size) {
    return (T[])(Array.newInstance(klass, size));

  }
  
  @SuppressWarnings("unchecked")
  public static <T> T[][] mkT2DArray(Class<?> klass, int[] dim ) {
	  if(dim.length != 2)
		  throw new RuntimeException("dim should be an array of size 2.");
	  return (T[][])(Array.newInstance(klass, dim));
  }

  @SuppressWarnings("unchecked")
  public static <T> List<T> sortedIfPossible(Collection<T> collection) {
    List<T> result = new ArrayList<>(collection);
    try {
      Collections.sort((List)result);
    } catch (ClassCastException e) {
      // unable to sort, just return the copy
    } catch (NullPointerException npe) {
      // this happens if there are null elements in the collection; just return the copy
    }
    return result;
  }
}

View on GitHub (pinned to 1b7edd19c4)