{"record":{"id":"07be0edbc0e2c5d9","repo":"stanfordnlp/CoreNLP","slug":"must-supply-a-target-label-to-compute-precision-an","errorCode":null,"errorMessage":"Must supply a target label to compute precision and recall against","messagePattern":"Must supply a target label to compute precision and recall against","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/classify/Classifier.java","lineNumber":40,"sourceCode":" */\n\npublic interface Classifier<L, F> extends Serializable {\n  public L classOf(Datum<L, F> example);\n\n  public Counter<L> scoresOf(Datum<L, F> example);\n\n  public Collection<L> labels();\n\n  /**\n   * Evaluates the precision and recall of this classifier against a dataset, and the target label.\n   *\n   * @param testData The dataset to evaluate the classifier on.\n   * @param targetLabel The target label (e.g., for relation extraction, this is the relation we're interested in).\n   * @return A pair of the precision (first) and recall (second) of the classifier on the target label.\n   */\n  public default Pair<Double, Double> evaluatePrecisionAndRecall(GeneralDataset<L, F> testData, L targetLabel) {\n    if (targetLabel == null) {\n      throw new IllegalArgumentException(\"Must supply a target label to compute precision and recall against\");\n    }\n    // Variables to count\n    int numCorrectAndTarget = 0;\n    int numTargetGuess = 0;\n    int numTargetGold = 0;\n    // Iterate over dataset\n    for (RVFDatum<L, F> datum : testData) {\n      // Get the gold label\n      L label = datum.label();\n      if (label == null) {\n        throw new IllegalArgumentException(\"Cannot compute precision and recall on unlabelled dataset. Offending datum: \" + datum);\n      }\n      // Get the guess label\n      L guess = classOf(datum);\n      // Compute statistics on datum\n      if (label.equals(targetLabel)) {\n        numTargetGold += 1;\n      }","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/classify/Classifier.java#L22-L58","documentation":"Classifier.evaluatePrecisionAndRecall computes precision/recall for a specific target label, which must be non-null since all statistics are keyed on comparisons against it. The library throws IllegalArgumentException immediately if the targetLabel argument is null, because no meaningful precision/recall can be computed without it.","triggerScenarios":"Calling evaluatePrecisionAndRecall(testData, null) directly, or via pr()/dumpAccuracy() where the target label variable was never initialized or was read from a missing config/property.","commonSituations":"Programmatic classifier evaluation where the target label is loaded from properties that omitted the relevant key; refactors that changed label types; generic evaluation loops passing null for labels not present in the dataset.","solutions":["Pass the actual label instance you want precision/recall for (it must equal a gold label in the dataset)","Verify the label constant is not null before calling (e.g. read from properties with a non-null default)","For full multi-class evaluation without a target label, use evaluateAccuracy instead"],"exampleFix":"// before\nPair<Double, Double> pr = classifier.evaluatePrecisionAndRecall(testData, null);\n// after\nL target = myTargetLabel; // e.g. dataset.labelIndex().get(\"RELATION_A\")\nPair<Double, Double> pr = classifier.evaluatePrecisionAndRecall(testData, target);","handlingStrategy":"validation","validationCode":"if (targetLabel == null) throw new IllegalArgumentException(\"targetLabel required before evaluatePrecisionAndRecall\");","typeGuard":"boolean hasLabel(L l) { return l != null; }","tryCatchPattern":"try { return clf.evaluatePrecisionAndRecall(test, target); } catch (IllegalArgumentException e) { log.error(\"Null/misconfigured target label\", e); return Pair.makePair(0.0, 0.0); }","preventionTips":["Resolve the target label from dataset.labelIndex() rather than hardcoding","Fail fast at config load time if the target label property is missing","Prefer evaluateAccuracy when no single target label applies"],"tags":["java","classification","null-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}