{"record":{"id":"ba76cfd75d2a177e","repo":"stanfordnlp/CoreNLP","slug":"dim-should-be-an-array-of-size-2","errorCode":null,"errorMessage":"dim should be an array of size 2.","messagePattern":"dim should be an array of size 2\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/ErasureUtils.java","lineNumber":50,"sourceCode":"\n  /**\n   * Makes an array based on klass, but casts it to be of type T[]. This is a very\n   * unsafe operation and should be used carefully. Namely, you should ensure that\n   * klass is a subtype of T, or that klass is a supertype of T *and* that the array\n   * will not escape the generic constant *and* that klass is the same as the erasure\n   * of T.\n   * @param <T>\n   */\n  @SuppressWarnings(\"unchecked\")\n  public static <T> T[] mkTArray(Class<?> klass, int size) {\n    return (T[])(Array.newInstance(klass, size));\n\n  }\n  \n  @SuppressWarnings(\"unchecked\")\n  public static <T> T[][] mkT2DArray(Class<?> klass, int[] dim ) {\n\t  if(dim.length != 2)\n\t\t  throw new RuntimeException(\"dim should be an array of size 2.\");\n\t  return (T[][])(Array.newInstance(klass, dim));\n  }\n\n  @SuppressWarnings(\"unchecked\")\n  public static <T> List<T> sortedIfPossible(Collection<T> collection) {\n    List<T> result = new ArrayList<>(collection);\n    try {\n      Collections.sort((List)result);\n    } catch (ClassCastException e) {\n      // unable to sort, just return the copy\n    } catch (NullPointerException npe) {\n      // this happens if there are null elements in the collection; just return the copy\n    }\n    return result;\n  }\n}\n","sourceCodeStart":32,"sourceCodeEnd":67,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/ErasureUtils.java#L32-L67","documentation":"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.","triggerScenarios":"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.","commonSituations":"Generic array-builder code shared between 1D and 2D cases passing the wrong dims; refactors changing array rank without updating dim construction.","solutions":["Pass exactly two dimensions, e.g. new int[]{rows, cols}.","Fix the code computing dim to produce two entries for 2D arrays.","Add a validation/assertion where dim is built so bad shapes fail at the source.","Use mkTArray (1D) or a different utility if the intended rank differs."],"exampleFix":"// before\nString[][] arr = ErasureUtils.mkT2DArray(String.class, new int[]{10});\n// after\nString[][] arr = ErasureUtils.mkT2DArray(String.class, new int[]{10, 20});","handlingStrategy":"validation","validationCode":"if (dim == null || dim.length != 2) throw new IllegalArgumentException(\"dim must have exactly 2 entries, got \" + (dim == null ? \"null\" : dim.length));","typeGuard":"boolean is2DDim(int[] dim) { return dim != null && dim.length == 2; }","tryCatchPattern":"try { T[][] arr = ErasureUtils.mkT2DArray(klass, dim); } catch (RuntimeException e) { /* fix dim shape or fall back to 1D utility */ }","preventionTips":["Construct dim with exactly two literals: new int[]{rows, cols}","Separate helpers for 1D vs 2D array creation","Assert rank at the point dims are computed","Avoid reusing dims arrays across different-rank utilities"],"tags":["java","reflection","arrays","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}