{"record":{"id":"229d30db0484739e","repo":"stanfordnlp/CoreNLP","slug":"can-t-have-null-components","errorCode":null,"errorMessage":"Can't have null components!","messagePattern":"Can't have null components!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/DataSeries.java","lineNumber":323,"sourceCode":"\n    }\n\n  }\n\n\n  // .......................................................................\n\n  public static class AverageDataSeries implements DataSeries {\n\n    private DataSeries[] components;\n\n    public AverageDataSeries(DataSeries[] components) {\n      if (components == null || components.length < 1)\n        throw new IllegalArgumentException(\"Need at least one component!\");\n      this.components = new DataSeries[components.length];\n      for (int i = 0; i < components.length; i++) {\n        if (components[i] == null)\n          throw new IllegalArgumentException(\"Can't have null components!\");\n        this.components[i] = components[i];\n      }\n      domain();                         // to ensure domains are same\n    }\n\n    public String name() {\n      StringBuilder name = new StringBuilder();\n      name.append(\"avg(\");\n      boolean flag = false;\n      for (DataSeries series : components) {\n        if (flag) name.append(\", \"); else flag = true;\n        name.append(series.name());\n      }\n      name.append(\")\");\n      return name.toString();\n    }\n\n    public double get(int i) {","sourceCodeStart":305,"sourceCodeEnd":341,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/DataSeries.java#L305-L341","documentation":"AverageDataSeries stores every component to later average and compare domains; null components cannot be averaged. The constructor loops over the array and throws IllegalArgumentException as soon as it encounters a null element.","triggerScenarios":"Calling `new AverageDataSeries(new DataSeries[]{seriesA, null, seriesC})` or passing an array produced by code that leaves null slots (e.g. pre-sized arrays partially filled).","commonSituations":"Arrays built with `new DataSeries[n]` and only some slots assigned, or optional data sources that returned null instead of skipping.","solutions":["Filter out null entries before constructing: use a List, add non-null series, then toArray.","Fix the producer code to omit null series instead of inserting null placeholders.","Validate the array with a null check loop before calling the constructor to give a clearer message."],"exampleFix":"// before\nDataSeries[] comps = new DataSeries[files.length]; // slots may stay null\nAverageDataSeries avg = new AverageDataSeries(comps);\n// after\nList<DataSeries> comps = new ArrayList<>();\nfor (DataSeries s : maybeNull) if (s != null) comps.add(s);\nAverageDataSeries avg = new AverageDataSeries(comps.toArray(new DataSeries[0]));","handlingStrategy":"validation","validationCode":"for (DataSeries s : components) {\n  if (s == null) throw new IllegalArgumentException(\"Null component in series array\");\n}\nAverageDataSeries avg = new AverageDataSeries(components);","typeGuard":"boolean allNonNull(DataSeries[] arr) { return arr != null && java.util.Arrays.stream(arr).allMatch(java.util.Objects::nonNull); }","tryCatchPattern":"try {\n  avg = new AverageDataSeries(components);\n} catch (IllegalArgumentException e) {\n  components = java.util.Arrays.stream(components).filter(java.util.Objects::nonNull).toArray(DataSeries[]::new);\n  avg = components.length > 0 ? new AverageDataSeries(components) : null;\n}","preventionTips":["Use List<DataSeries> and add only non-null series instead of pre-sized arrays.","Never insert null placeholders into series arrays.","Filter with Objects::nonNull before construction."],"tags":["null-pointer","constructor","validation"],"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"}