{"record":{"id":"508af9769248154e","repo":"stanfordnlp/CoreNLP","slug":"map-must-have-at-least-one-element-to-infer-numeri","errorCode":null,"errorMessage":"Map must have at least one element to infer numeric type; add an element first or use e.g. fromMap(map, Integer.class)","messagePattern":"Map must have at least one element to infer numeric type; add an element first or use e\\.g\\. fromMap\\(map, Integer\\.class\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/Counters.java","lineNumber":2602,"sourceCode":"  public static <E> Counter<E> asCounter(FixedPrioritiesPriorityQueue<E> p) {\n    FixedPrioritiesPriorityQueue<E> pq = p.clone();\n    ClassicCounter<E> counter = new ClassicCounter<>();\n    while (pq.hasNext()) {\n      double priority = pq.getPriority();\n      E element = pq.next();\n      counter.incrementCount(element, priority);\n    }\n    return counter;\n  }\n\n  /**\n   * Returns a counter view of the given map. Infers the numeric type of the\n   * values from the first element in map.values().\n   */\n  @SuppressWarnings(\"unchecked\")\n  public static <E, N extends Number> Counter<E> fromMap(final Map<E, N> map) {\n    if (map.isEmpty()) {\n      throw new IllegalArgumentException(\"Map must have at least one element\" + \" to infer numeric type; add an element first or use e.g.\" + \" fromMap(map, Integer.class)\");\n    }\n    return fromMap(map, (Class) map.values().iterator().next().getClass());\n  }\n\n  /**\n   * Returns a counter view of the given map. The type parameter is the type of\n   * the values in the map, which because of Java's generics type erasure, can't\n   * be discovered by reflection if the map is currently empty.\n   */\n  public static <E, N extends Number> Counter<E> fromMap(final Map<E, N> map, final Class<N> type) {\n    // get our initial total\n    double initialTotal = 0.0;\n    for (Map.Entry<E, N> entry : map.entrySet()) {\n      initialTotal += entry.getValue().doubleValue();\n    }\n\n    // and pass it in to the returned inner class with a final variable\n    final double initialTotalFinal = initialTotal;","sourceCodeStart":2584,"sourceCodeEnd":2620,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/Counters.java#L2584-L2620","documentation":"Counters.fromMap(Map<E,N>) returns a Counter view of a Map and infers the value's numeric type (Integer, Double, Long, ...) from the first element of map.values(). The library throws this IllegalArgumentException when the map is empty because there is no element from which to infer the numeric class.","triggerScenarios":"Calling Counters.fromMap(someMap) where someMap.isEmpty() is true.","commonSituations":"Result of an empty DB/file parse passed straight to fromMap; initializing a counter before loading data; a filtered map that ended up with zero entries at runtime.","solutions":["Use the explicit-type overload: Counters.fromMap(map, Double.class) (or Integer.class etc.)","Guard with !map.isEmpty() before calling the inferring overload","Initialize the map with a default/seed entry if an empty map is legitimate"],"exampleFix":"// before\nCounter<String> c = Counters.fromMap(map); // throws if map is empty\n// after\nCounter<String> c = map.isEmpty()\n    ? new ClassicCounter<>()\n    : Counters.fromMap(map, Double.class);","handlingStrategy":"validation","validationCode":"Counter<E> c = map.isEmpty()\n    ? new ClassicCounter<>()\n    : Counters.fromMap(map, Double.class); // explicit type, no inference needed","typeGuard":"boolean canInfer = map != null && !map.isEmpty();","tryCatchPattern":"try {\n  return Counters.fromMap(map);\n} catch (IllegalArgumentException e) {\n  log.warn(\"Empty map, returning empty counter\");\n  return new ClassicCounter<>();\n}","preventionTips":["Always prefer the explicit fromMap(map, Class) overload","Check map size after filtering/parsing steps before conversion","Return ClassicCounter.EMPTY or a fresh counter for empty inputs"],"tags":["java","counter","empty-collection","type-inference"],"backgroundTag":"empty-required-field","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"}