{"record":{"id":"ddcb71be9bbf704c","repo":"stanfordnlp/CoreNLP","slug":"unexpected-row-length-in-line","errorCode":null,"errorMessage":"Unexpected row length in line ","messagePattern":"Unexpected row length in line ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/neural/NeuralUtils.java","lineNumber":78,"sourceCode":"  public static List<SimpleMatrix> loadTextMatrices(String path) {\n    List<SimpleMatrix> matrices = new ArrayList<>();\n    for (String mString : IOUtils.stringFromFile(path).trim().split(\"\\n\\n\")) {\n      matrices.add(NeuralUtils.convertTextMatrix(mString).transpose());\n    }\n    return matrices;\n  }\n\n  public static SimpleMatrix convertTextMatrix(String text) {\n    List<String> lines = CollectionUtils.filterAsList(Arrays.asList(text.split(\"\\n\")),\n            s -> ! s.trim().isEmpty());\n    int numRows = lines.size();\n    int numCols = lines.get(0).trim().split(\"\\\\s+\").length;\n    double[][] data = new double[numRows][numCols];\n    for (int row = 0; row < numRows; ++row) {\n      String line = lines.get(row);\n      String[] pieces = line.trim().split(\"\\\\s+\");\n      if (pieces.length != numCols) {\n        throw new RuntimeException(\"Unexpected row length in line \" + row);\n      }\n      for (int col = 0; col < numCols; ++col) {\n        data[row][col] = Double.valueOf(pieces[col]);\n      }\n    }\n    return new SimpleMatrix(data);\n  }\n\n  /**\n   * @param matrix The matrix to return as a String\n   * @param format The format to use for each value in the matrix, eg \"%f\"\n   */\n  public static String toString(SimpleMatrix matrix, String format) {\n    ByteArrayOutputStream stream = new ByteArrayOutputStream();\n    MatrixIO.print(new PrintStream(stream), (DMatrix) matrix.getMatrix(), format);\n    return stream.toString();\n  }\n","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/neural/NeuralUtils.java#L60-L96","documentation":"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.","triggerScenarios":"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.","commonSituations":"Hand-edited matrix files; files exported from spreadsheets with inconsistent delimiters; accidentally including a header row; truncated last line.","solutions":["Fix the malformed row (index is given in the message) so all rows have the same number of numeric columns","Remove header/comment/blank lines that break the uniform column count","Re-export the matrix with a consistent single delimiter (single space or tab)"],"exampleFix":"// before (file)\n1.0 2.0\n3.0\n// after (file)\n1.0 2.0\n3.0 4.0","handlingStrategy":"validation","validationCode":"List<String> lines = Files.readAllLines(path);\nint n = lines.get(0).trim().split(\"\\\\s+\").length;\nfor (int i = 1; i < lines.size(); i++)\n  if (lines.get(i).trim().split(\"\\\\s+\").length != n)\n    throw new IllegalStateException(\"ragged row \" + i);","typeGuard":null,"tryCatchPattern":"try { SimpleMatrix m = NeuralUtils.loadTextMatrix(file); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"Unexpected row length\")) { /* sanitize and retry */ } else throw e; }","preventionTips":["Pre-sanitize files: strip headers, comments, blank lines, trailing whitespace","Export with a fixed delimiter and fixed precision","Validate rectangularity before parsing large generated matrices"],"tags":["java","stanford-nlp","matrix","file-format"],"backgroundTag":"invalid-argument-format","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"}