{"record":{"id":"63d5197ed438b911","repo":"stanfordnlp/CoreNLP","slug":"r-must-not-be-null","errorCode":null,"errorMessage":"r must not be null!","messagePattern":"r must not be null!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/SimpleGoodTuring.java","lineNumber":47,"sourceCode":"  private double slope;\n  private double intercept;\n  private double[] z;\n  private double[] logR;\n  private double[] logZ;\n  private double[] rStar;\n  private double[] p;\n\n  /**\n   * Each instance of this class encapsulates the computation of the smoothing\n   * for one probability distribution.  The constructor takes two arguments\n   * which are two parallel arrays.  The first is an array of counts, which must\n   * be positive and in ascending order.  The second is an array of\n   * corresponding counts of counts; that is, for each i, n[i] represents the\n   * number of types which occurred with count r[i] in the underlying\n   * collection.  See the documentation for main() for a concrete example.\n   */\n  public SimpleGoodTuring(int[] r, int[] n) {\n    if (r == null) throw new IllegalArgumentException(\"r must not be null!\");\n    if (n == null) throw new IllegalArgumentException(\"n must not be null!\");\n    if (r.length != n.length) throw new IllegalArgumentException(\"r and n must have same size!\");\n    if (r.length < MIN_INPUT) throw new IllegalArgumentException(\"r must have size >= \" + MIN_INPUT + \"!\");\n    this.r = new int[r.length];\n    this.n = new int[n.length];\n    System.arraycopy(r, 0, this.r, 0, r.length); // defensive copy\n    System.arraycopy(n, 0, this.n, 0, n.length); // defensive copy\n    this.rows = r.length;\n    compute();\n    validate(TOLERANCE);\n  }\n\n  /**\n   * Returns the probability allocated to types not seen in the underlying\n   * collection.\n   */\n  public double getProbabilityForUnseen() {\n    return pZero;","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/SimpleGoodTuring.java#L29-L65","documentation":"SimpleGoodTuring implements Simple Good-Turing smoothing from counts-of-counts arrays; the constructor validates its inputs up front and throws IllegalArgumentException if the r array (frequency values) is null. This fail-fast check prevents NullPointerExceptions deep in the smoothing computation.","triggerScenarios":"new SimpleGoodTuring(null, n) — passing a null r array, typically when frequency data failed to load or a variable was never initialized.","commonSituations":"Config/pipeline mistakes where the counts file wasn't read and arrays stayed null; refactors that dropped an array initialization; conditional data-loading that silently skipped population of r.","solutions":["Ensure r is populated (non-null, positive, ascending) before constructing","Add a null/empty check at the data-loading site and fail there with a clearer message","Check why the frequency array source returned null (missing file, empty parse)"],"exampleFix":"// before\nSimpleGoodTuring sgt = new SimpleGoodTuring(r, n);\n// after\nif (r == null || r.length == 0) throw new IllegalStateException(\"frequency array not loaded\");\nSimpleGoodTuring sgt = new SimpleGoodTuring(r, n);","handlingStrategy":"validation","validationCode":"if (r == null || r.length < 2) throw new IllegalStateException(\"frequency array r missing or too small\");","typeGuard":"boolean hasValidR(int[] r) { return r != null && r.length >= 2 && Arrays.stream(r).allMatch(v -> v > 0); }","tryCatchPattern":"try {\n  return new SimpleGoodTuring(r, n);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"r must not be null\")) {\n    return defaultSmoothing(); // fall back to unsmoothed/add-k estimates\n  }\n  throw e;\n}","preventionTips":["Fail with a clear message at data-loading time when arrays are null","Initialize arrays at declaration to avoid silent nulls","Unit-test the counts-loading path with missing-file scenarios"],"tags":["java","null-check","smoothing"],"backgroundTag":"null-argument","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"}