stanfordnlp/CoreNLP · error · InvalidElementException

vectorName + " element " + i + " is " + vector[i]

Error message

vectorName + " element " + i + " is " + vector[i]

What it means

ArrayMath.assertFinite(double[] vector, String vectorName) verifies every element is a normal finite double. If any element is NaN or +/-Infinity it throws InvalidElementException (a RuntimeException subclass) naming the vector, index, and offending value, so numerical corruption is caught at the boundary instead of propagating silently.

Solutions

  1. Find the source of the NaN/Infinity using the index and value in the message, and fix the upstream arithmetic
  2. Pre-sanitize the vector: replace non-finite values or clamp them before assertFinite
  3. Skip assertFinite for intentionally non-finite data and validate only where finiteness is required

Example fix

// before
ArrayMath.assertFinite(vector, "weights"); // throws: weights element 7 is NaN
// after
for (int i = 0; i < vector.length; i++) {
  if (!Double.isFinite(vector[i])) vector[i] = 0.0; // or log & fix upstream
}
ArrayMath.assertFinite(vector, "weights");
Defensive patterns

Strategy: try-catch

Validate before calling

boolean finite = true;
for (double v : vector) { if (Double.isNaN(v) || Double.isInfinite(v)) { finite = false; break; } }

Type guard

static boolean isFiniteVector(double[] v) {
  for (double d : v) if (Double.isNaN(d) || Double.isInfinite(d)) return false;
  return true;
}

Try / catch

try {
  ArrayMath.assertFinite(vector, "weights");
} catch (ArrayMath.InvalidElementException e) {
  log.error("non-finite vector: " + e.getMessage(), e);
  // quarantine/recompute the vector
}

Prevention

When it happens

Trigger: Calling assertFinite on a vector produced by division by zero (Infinity), 0/0 (NaN), or overflow from exp/log, e.g. validating model weights or gradient arrays before use.

Common situations: Degenerate training data, learning rates too large causing overflow, uninitialized arrays filled with NaN, or failed normalization producing division by zero.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/1e39c1a4508bbd9e. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/math/ArrayMath.java:2134

  public static void multiplyInto(double[] a, double[] b, double c) {
    for (int i=0; i<a.length; i++) {
      a[i] = b[i] * c;
    }
  }

  public static double entropy(double[] probs) {
    double e = 0.0;
    for (double p : probs) {
      if (p != 0.0)
        e -= p * Math.log(p);
    }
    return e;
  }

  public static void assertFinite(double[] vector, String vectorName) throws InvalidElementException {
    for(int i=0; i<vector.length; i++){
      if (Double.isNaN(vector[i]) || Double.isInfinite(vector[i])) {
        throw new InvalidElementException(vectorName + " element " + i + " is " + vector[i]);
      }
    }
  }

  public static class InvalidElementException extends RuntimeException {

    private static final long serialVersionUID = 1647150702529757545L;

    public InvalidElementException(String s) {
      super(s);
    }
  }

}

View on GitHub (pinned to 1b7edd19c4)