{"record":{"id":"e3975aadc685a3b8","repo":"stanfordnlp/CoreNLP","slug":"n-0-n","errorCode":null,"errorMessage":"n < 0: ${n}","messagePattern":"n < 0: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/CollectionUtils.java","lineNumber":367,"sourceCode":"   * @param n The number of samples to take\n   * @return a new collection with the sample\n   */\n  public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n) {\n    return sampleWithoutReplacement(c, n, new Random());\n  }\n\n  /**\n   * Samples without replacement from a collection, using your own\n   * {@link Random} number generator.\n   *\n   * @param c The collection to be sampled from\n   * @param n The number of samples to take\n   * @param r The random number generator\n   * @return a new collection with the sample\n   */\n  public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n, Random r) {\n    if (n < 0)\n      throw new IllegalArgumentException(\"n < 0: \" + n);\n    if (n > c.size())\n      throw new IllegalArgumentException(\"n > size of collection: \" + n + \", \" + c.size());\n    List<E> copy = new ArrayList<>(c.size());\n    copy.addAll(c);\n    Collection<E> result = new ArrayList<>(n);\n    for (int k = 0; k < n; k++) {\n      double d = r.nextDouble();\n      int x = (int) (d * copy.size());\n      result.add(copy.remove(x));\n    }\n    return result;\n  }\n\n  public static <E> E sample(List<E> l, Random r) {\n    int i = r.nextInt(l.size());\n    return l.get(i);\n  }\n","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/CollectionUtils.java#L349-L385","documentation":"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.","triggerScenarios":"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.","commonSituations":"Downstream code passing a difference that underflowed to negative; misconfigured sample-size parameters read from properties or CLI args.","solutions":["Ensure n >= 0 before calling: clamp with Math.max(0, n).","If n derives from c.size() - k, clamp the difference: Math.max(0, c.size() - k).","Fix the caller/config producing the negative value; log the input for diagnosis.","For 'all elements', pass c.size() rather than a computed value."],"exampleFix":"// before\nCollection<E> s = CollectionUtils.sampleWithoutReplacement(c, c.size() - k, r);\n// after\nint n = Math.max(0, c.size() - k);\nCollection<E> s = CollectionUtils.sampleWithoutReplacement(c, n, r);","handlingStrategy":"validation","validationCode":"if (n < 0) throw new IllegalArgumentException(\"sample size must be >= 0, got \" + n);","typeGuard":"boolean isValidSampleSize(int n) { return n >= 0; }","tryCatchPattern":"try { return CollectionUtils.sampleWithoutReplacement(c, n, r); } catch (IllegalArgumentException e) { return new ArrayList<>(); }","preventionTips":["Clamp differences like c.size() - k with Math.max(0, ...)","Validate sample sizes parsed from config/CLI before use","Unit-test sampling helpers with boundary values 0, size, size+1, negative"],"tags":["java","collections","sampling","argument-validation"],"backgroundTag":"argument-out-of-range","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"}