stanfordnlp/CoreNLP · error · IllegalArgumentException

n < 0

Error message

n < 0: ${n}

What it means

CollectionUtils.sampleWithoutReplacement throws IllegalArgumentException when the requested sample count n is negative, since a sample cannot have fewer than 0 elements. This is an argument precheck before any sampling work.

Solutions

  1. Ensure n >= 0 before calling: clamp with Math.max(0, n).
  2. If n derives from c.size() - k, clamp the difference: Math.max(0, c.size() - k).
  3. Fix the caller/config producing the negative value; log the input for diagnosis.
  4. For 'all elements', pass c.size() rather than a computed value.

Example fix

// before
Collection<E> s = CollectionUtils.sampleWithoutReplacement(c, c.size() - k, r);
// after
int n = Math.max(0, c.size() - k);
Collection<E> s = CollectionUtils.sampleWithoutReplacement(c, n, r);
Defensive patterns

Strategy: validation

Validate before calling

if (n < 0) throw new IllegalArgumentException("sample size must be >= 0, got " + n);

Type guard

boolean isValidSampleSize(int n) { return n >= 0; }

Try / catch

try { return CollectionUtils.sampleWithoutReplacement(c, n, r); } catch (IllegalArgumentException e) { return new ArrayList<>(); }

Prevention

When it happens

Trigger: Calling sampleWithoutReplacement(c, n, r) with n < 0, e.g. from size computations like c.size() - k where k > c.size(), or user/config input passed unvalidated.

Common situations: Downstream code passing a difference that underflowed to negative; misconfigured sample-size parameters read from properties or CLI args.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/util/CollectionUtils.java:367

   * @param n The number of samples to take
   * @return a new collection with the sample
   */
  public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n) {
    return sampleWithoutReplacement(c, n, new Random());
  }

  /**
   * Samples without replacement from a collection, using your own
   * {@link Random} number generator.
   *
   * @param c The collection to be sampled from
   * @param n The number of samples to take
   * @param r The random number generator
   * @return a new collection with the sample
   */
  public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n, Random r) {
    if (n < 0)
      throw new IllegalArgumentException("n < 0: " + n);
    if (n > c.size())
      throw new IllegalArgumentException("n > size of collection: " + n + ", " + c.size());
    List<E> copy = new ArrayList<>(c.size());
    copy.addAll(c);
    Collection<E> result = new ArrayList<>(n);
    for (int k = 0; k < n; k++) {
      double d = r.nextDouble();
      int x = (int) (d * copy.size());
      result.add(copy.remove(x));
    }
    return result;
  }

  public static <E> E sample(List<E> l, Random r) {
    int i = r.nextInt(l.size());
    return l.get(i);
  }

View on GitHub (pinned to 1b7edd19c4)