{"record":{"id":"4979a6f9e387bcd2","repo":"stanfordnlp/CoreNLP","slug":"cannot-sum-attribute-key-object-of-type","errorCode":null,"errorMessage":"Cannot sum attribute \" + key + \", object of type: \" + obj.getClass()","messagePattern":"Cannot sum attribute \" \\+ key \\+ \", object of type: \" \\+ obj\\.getClass\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/pipeline/CoreMapAttributeAggregator.java","lineNumber":182,"sourceCode":"  public static final CoreMapAttributeAggregator CONCAT_TEXT = new ConcatTextAggregator(\" \");\n  public static final CoreMapAttributeAggregator COUNT = new CoreMapAttributeAggregator() {\n    public Object aggregate(Class key, List<? extends CoreMap> in) {\n      return in.size();\n    }\n  };\n  public static final CoreMapAttributeAggregator SUM = new CoreMapAttributeAggregator() {\n    public Object aggregate(Class key, List<? extends CoreMap> in) {\n      if (in == null) return null;\n      double sum = 0;\n      for (CoreMap cm:in) {\n        Object obj = cm.get(key);\n        if (obj != null) {\n          if (obj instanceof Number) {\n            sum += ((Number) obj).doubleValue();\n          } else if (obj instanceof String) {\n            sum += Double.parseDouble((String) obj);\n          } else {\n            throw new RuntimeException(\"Cannot sum attribute \" + key + \", object of type: \" + obj.getClass());\n          }\n        }\n      }\n      return sum;\n    }\n  };\n  public static final CoreMapAttributeAggregator MIN = new CoreMapAttributeAggregator() {\n    public Object aggregate(Class key, List<? extends CoreMap> in) {\n      if (in == null) return null;\n      Comparable min = null;\n      for (CoreMap cm:in) {\n        Object obj = cm.get(key);\n        if (obj != null) {\n          if (obj instanceof Comparable) {\n            Comparable c = (Comparable) obj;\n            if (min == null) {\n              min = c;\n            } else if (c.compareTo(min) < 0) {","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/pipeline/CoreMapAttributeAggregator.java#L164-L200","documentation":"CoreMapAttributeAggregator's SUM aggregator iterates values for the attribute key and only knows how to convert Number and String values to doubles. Any other object type (or an unparseable case handled upstream) triggers a RuntimeException because summation is undefined for it.","triggerScenarios":"Calling aggregate with the SUM aggregator over a CoreMap attribute whose stored value is neither Number nor String, e.g. a List<Integer>, Boolean, or custom object stored under that key.","commonSituations":"Aggregating tokens/sentences where a key holds composite values (lists of spans, Trees, CoreLabels); expecting aggregation to descend into lists which it does not.","solutions":["Use an aggregator key whose values are numeric (Numbers or numeric Strings).","Extract the numeric field yourself before aggregating (map values to Double first).","Choose a different aggregation (e.g. a custom Aggregator) that handles the actual value type."],"exampleFix":"// before\nDouble total = aggregator.aggregate(coremaps, CoreAnnotations.TextAnnotation.class); // Text is String, ok, but a List value would fail\n// after\nDouble total = coremaps.stream().map(m -> m.get(CoreAnnotations.OrdinalAnnotation.class)).filter(Objects::nonNull).mapToDouble(n -> ((Number) n).doubleValue()).sum();","handlingStrategy":"type-guard","validationCode":"Object v = map.get(key);\nif (v != null && !(v instanceof Number) && !(v instanceof String)) throw new IllegalStateException(\"Non-numeric value under key\");","typeGuard":"static boolean isSummable(Object o) { return o instanceof Number || (o instanceof String && isNumeric((String) o)); }","tryCatchPattern":"try { sum = aggregator.aggregate(maps, key); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"Cannot sum attribute\")) { sum = manualNumericSum(maps, key); } else { throw e; } }","preventionTips":["Only attach Number or numeric-String values to attributes you plan to sum.","Unwrap lists/custom objects into scalars before aggregating.","Document which keys are aggregation-safe in your pipeline code."],"tags":["stanford-corenlp","aggregation","type-mismatch"],"backgroundTag":"type-mismatch","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"}