{"record":{"id":"d71fb50255c9ab35","repo":"stanfordnlp/CoreNLP","slug":"n-must-not-be-null","errorCode":null,"errorMessage":"n must not be null!","messagePattern":"n must not be null!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/SimpleGoodTuring.java","lineNumber":48,"sourceCode":"  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;\n  }","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/SimpleGoodTuring.java#L30-L66","documentation":"Same constructor validation as the r check: SimpleGoodTuring requires a non-null n array (counts of counts) and throws IllegalArgumentException immediately if it is null, since smoothing cannot proceed without it.","triggerScenarios":"new SimpleGoodTuring(r, null) — the counts-of-counts array was never loaded or initialized.","commonSituations":"File/DB load failures leaving n null while r was populated; copy-paste errors initializing only one of the two arrays.","solutions":["Ensure n is loaded and non-null before construction","Validate both arrays at the loading site with a single check","Fix the data source that returned null for the counts-of-counts"],"exampleFix":"// before\nSimpleGoodTuring sgt = new SimpleGoodTuring(r, n);\n// after\nif (n == null || n.length == 0) throw new IllegalStateException(\"counts-of-counts not loaded\");\nSimpleGoodTuring sgt = new SimpleGoodTuring(r, n);","handlingStrategy":"validation","validationCode":"if (n == null || n.length < 2) throw new IllegalStateException(\"counts-of-counts array n missing or too small\");","typeGuard":"boolean hasValidN(int[] n) { return n != null && n.length >= 2 && Arrays.stream(n).allMatch(v -> v > 0); }","tryCatchPattern":"try {\n  return new SimpleGoodTuring(r, n);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"n must not be null\")) {\n    return defaultSmoothing();\n  }\n  throw e;\n}","preventionTips":["Load r and n together from one source so neither can be null alone","Check file/DB load results before construction","Validate both arrays in a single guard helper"],"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"}