{"record":{"id":"b6e1a1ef8097ffac","repo":"stanfordnlp/CoreNLP","slug":"too-many-columns-columni-numcolumns-offset","errorCode":null,"errorMessage":"Too many columns: <columnI>/<numColumns> (offset: <offset>)","messagePattern":"Too many columns: <columnI>/<numColumns> \\(offset: <offset>\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/io/IOUtils.java","lineNumber":1451,"sourceCode":"    //--Read\n    for(int offset=0; offset<csvContents.length; offset++){\n      if(nextIsEscaped){\n        buffer[columnI].append(csvContents[offset]);\n        nextIsEscaped = false;\n      } else {\n        switch(csvContents[offset]){\n          case '\"':\n            //(case: quotes)\n            inQuotes = !inQuotes;\n            break;\n          case ',':\n            //(case: field separator)\n            if(inQuotes){\n              buffer[columnI].append(',');\n            } else {\n              columnI += 1;\n              if(columnI >= numColumns){\n                throw new IllegalArgumentException(\"Too many columns: \"+columnI+\"/\"+numColumns+\" (offset: \" + offset + \")\");\n              }\n              buffer[columnI] = new StringBuilder();\n            }\n            break;\n          case '\\n':\n            //(case: newline)\n            if(inQuotes){\n              buffer[columnI].append('\\n');\n            } else {\n              //((error checks))\n              if(columnI != numColumns-1){\n                throw new IllegalArgumentException(\"Too few columns: \"+columnI+\"/\"+numColumns+\" (offset: \" + offset + \")\");\n              }\n              //((create line))\n              String[] rtn = new String[buffer.length];\n              for(int i=0; i<buffer.length; i++){ rtn[i] = buffer[i].toString(); }\n              lines.add(rtn);\n              //((update state))","sourceCodeStart":1433,"sourceCodeEnd":1469,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/io/IOUtils.java#L1433-L1469","documentation":"Thrown by IOUtils' CSV/TSV line parser (csvStringToColumns-style readers) when a line contains MORE comma-separated fields than the declared number of columns. While parsing a non-quoted line, encountering a comma that would advance columnI past numColumns-1 raises this IllegalArgumentException with column counts and the character offset in the input.","triggerScenarios":"Calling the CSV reader (e.g. slurpFileAsColumns / csvStringToColumns variants) with a numColumns argument smaller than the actual field count of some row: unquoted commas inside field values, a header declaring fewer columns than data rows, or a wrong numColumns constant in the caller.","commonSituations":"CSV data exported from Excel with commas inside text that was not quote-escaped; a pipeline expects 3 columns but source data gained a 4th; parsing a TSV file while passing ',' as separator mismatch; hard-coded numColumns not updated after schema change.","solutions":["Increase the numColumns argument to match the actual maximum number of comma-separated fields per row.","Quote fields that contain literal commas in the source data (standard CSV quoting) so the parser treats them as part of one field.","Pre-validate the file: count fields per line (outside quotes) and reject/report offending lines before calling the parser.","If the delimiter is not a comma, use the correct parsing method/separator instead."],"exampleFix":"// before\nList<String[]> cols = IOUtils.csvStringToColumns(csvText, 3);\n// after\nint maxFields = csvText.lines().mapToInt(l -> l.split(\",(?=(?:[^\"]*[^\"]*\")*[^\"]*$)\").length).max().orElse(0);\nList<String[]> cols = IOUtils.csvStringToColumns(csvText, maxFields);","handlingStrategy":"validation","validationCode":"int expectedCols = numColumns;\nint lineNo = 0;\nfor (String line : csvText.split(\"\\n\")) {\n  lineNo++;\n  if (countFields(line) > expectedCols)\n    throw new IllegalArgumentException(\"Line \" + lineNo + \" has too many fields (unquoted comma?)\");\n}\nstatic int countFields(String line) {\n  int n = 1; boolean inQ = false;\n  for (char c : line.toCharArray()) {\n    if (c == '\"') inQ = !inQ;\n    else if (c == ',' && !inQ) n++;\n  }\n  return n;\n}","typeGuard":null,"tryCatchPattern":"try {\n  List<String[]> cols = IOUtils.csvStringToColumns(csvText, numColumns);\n} catch (IllegalArgumentException e) {\n  // e.getMessage() contains columnI/numColumns and offset; report line/offset to data owner\n  throw new DataFormatException(\"Bad CSV row: \" + e.getMessage());\n}","preventionTips":["Quote any field containing a comma in source data.","Derive numColumns from the data (max fields per row) instead of hard-coding.","Keep a schema check in CI for pipeline inputs."],"tags":["csv","parsing","validation","java"],"backgroundTag":"schema-validation-failed","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"}