stanfordnlp/CoreNLP · error · RuntimeException

Unexpected row length in line

Error message

Unexpected row length in line 

What it means

NeuralUtils.convertTextMatrix parses a whitespace-delimited text matrix into a SimpleMatrix. It infers the column count from the first line and requires every subsequent row to have exactly that many fields; any row with a different count makes the matrix ragged, so it throws this RuntimeException naming the offending row index.

Solutions

  1. Fix the malformed row (index is given in the message) so all rows have the same number of numeric columns
  2. Remove header/comment/blank lines that break the uniform column count
  3. Re-export the matrix with a consistent single delimiter (single space or tab)

Example fix

// before (file)
1.0 2.0
3.0
// after (file)
1.0 2.0
3.0 4.0
Defensive patterns

Strategy: validation

Validate before calling

List<String> lines = Files.readAllLines(path);
int n = lines.get(0).trim().split("\\s+").length;
for (int i = 1; i < lines.size(); i++)
  if (lines.get(i).trim().split("\\s+").length != n)
    throw new IllegalStateException("ragged row " + i);

Try / catch

try { SimpleMatrix m = NeuralUtils.loadTextMatrix(file); } catch (RuntimeException e) { if (e.getMessage().startsWith("Unexpected row length")) { /* sanitize and retry */ } else throw e; }

Prevention

When it happens

Trigger: Calling NeuralUtils.loadTextMatrix on a file where line N has more or fewer whitespace-separated numbers than line 0 — e.g. stray spaces, a missing value, or a header/comment line inside the data.

Common situations: Hand-edited matrix files; files exported from spreadsheets with inconsistent delimiters; accidentally including a header row; truncated last line.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/neural/NeuralUtils.java:78

  public static List<SimpleMatrix> loadTextMatrices(String path) {
    List<SimpleMatrix> matrices = new ArrayList<>();
    for (String mString : IOUtils.stringFromFile(path).trim().split("\n\n")) {
      matrices.add(NeuralUtils.convertTextMatrix(mString).transpose());
    }
    return matrices;
  }

  public static SimpleMatrix convertTextMatrix(String text) {
    List<String> lines = CollectionUtils.filterAsList(Arrays.asList(text.split("\n")),
            s -> ! s.trim().isEmpty());
    int numRows = lines.size();
    int numCols = lines.get(0).trim().split("\\s+").length;
    double[][] data = new double[numRows][numCols];
    for (int row = 0; row < numRows; ++row) {
      String line = lines.get(row);
      String[] pieces = line.trim().split("\\s+");
      if (pieces.length != numCols) {
        throw new RuntimeException("Unexpected row length in line " + row);
      }
      for (int col = 0; col < numCols; ++col) {
        data[row][col] = Double.valueOf(pieces[col]);
      }
    }
    return new SimpleMatrix(data);
  }

  /**
   * @param matrix The matrix to return as a String
   * @param format The format to use for each value in the matrix, eg "%f"
   */
  public static String toString(SimpleMatrix matrix, String format) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    MatrixIO.print(new PrintStream(stream), (DMatrix) matrix.getMatrix(), format);
    return stream.toString();
  }

View on GitHub (pinned to 1b7edd19c4)