stanfordnlp/CoreNLP · error · IllegalArgumentException

r and n must have same size!

Error message

r and n must have same size!

What it means

SimpleGoodTuring requires the r (frequencies) and n (counts of counts) arrays to be parallel — element i of n corresponds to r[i] — so the constructor throws IllegalArgumentException when their lengths differ, preventing index corruption during smoothing.

Solutions

  1. Verify r.length == n.length before constructing; zip them from a single source so they can't diverge
  2. Filter both arrays together (same predicate) instead of independently
  3. Re-derive n directly from r's source so they share one provenance

Example fix

// before
SimpleGoodTuring sgt = new SimpleGoodTuring(freqs, counts);
// after
if (freqs.length != counts.length) {
  throw new IllegalArgumentException("freqs.length=" + freqs.length + " counts.length=" + counts.length);
}
SimpleGoodTuring sgt = new SimpleGoodTuring(freqs, counts);
Defensive patterns

Strategy: validation

Validate before calling

if (r.length != n.length) throw new IllegalStateException("r and n lengths differ: " + r.length + " vs " + n.length);

Type guard

boolean sameLength(int[] a, int[] b) { return a != null && b != null && a.length == b.length; }

Try / catch

try {
  return new SimpleGoodTuring(r, n);
} catch (IllegalArgumentException e) {
  if (e.getMessage() != null && e.getMessage().contains("same size")) {
    int m = Math.min(r.length, n.length);
    return new SimpleGoodTuring(Arrays.copyOf(r, m), Arrays.copyOf(n, m));
  }
  throw e;
}

Prevention

When it happens

Trigger: new SimpleGoodTuring(r, n) with r.length != n.length — e.g. loading the two columns from separate sources where one has extra/missing entries, or filtering one array but not the other.

Common situations: Parsing a two-column counts file where blank lines were handled inconsistently; truncating one array; merging histograms from multiple runs and updating only one array.

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

Appendix: source

Thrown at src/edu/stanford/nlp/stats/SimpleGoodTuring.java:49

  private double[] z;
  private double[] logR;
  private double[] logZ;
  private double[] rStar;
  private double[] p;

  /**
   * Each instance of this class encapsulates the computation of the smoothing
   * for one probability distribution.  The constructor takes two arguments
   * which are two parallel arrays.  The first is an array of counts, which must
   * be positive and in ascending order.  The second is an array of
   * corresponding counts of counts; that is, for each i, n[i] represents the
   * number of types which occurred with count r[i] in the underlying
   * collection.  See the documentation for main() for a concrete example.
   */
  public SimpleGoodTuring(int[] r, int[] n) {
    if (r == null) throw new IllegalArgumentException("r must not be null!");
    if (n == null) throw new IllegalArgumentException("n must not be null!");
    if (r.length != n.length) throw new IllegalArgumentException("r and n must have same size!");
    if (r.length < MIN_INPUT) throw new IllegalArgumentException("r must have size >= " + MIN_INPUT + "!");
    this.r = new int[r.length];
    this.n = new int[n.length];
    System.arraycopy(r, 0, this.r, 0, r.length); // defensive copy
    System.arraycopy(n, 0, this.n, 0, n.length); // defensive copy
    this.rows = r.length;
    compute();
    validate(TOLERANCE);
  }

  /**
   * Returns the probability allocated to types not seen in the underlying
   * collection.
   */
  public double getProbabilityForUnseen() {
    return pZero;
  }

View on GitHub (pinned to 1b7edd19c4)