我在scala代码中使用了supercsv库
for (row <- readyData) { // tried foreach and map too
listWriter.write(row)
}
其中readyData
为Seq[java.util.List[String]]
,listWriter
为CsvListWriter
。我有时会得到
java.lang.StringIndexOutOfBoundsException: String index out of range: -2
at java.lang.String.<init>(String.java:197)
at java.lang.StringBuilder.toString(StringBuilder.java:405)
at org.supercsv.encoder.DefaultCsvEncoder.encode(DefaultCsvEncoder.java:93)
at org.supercsv.io.AbstractCsvWriter.escapeString(AbstractCsvWriter.java:102)
at org.supercsv.io.AbstractCsvWriter.writeRow(AbstractCsvWriter.java:196)
at org.supercsv.io.AbstractCsvWriter.writeRow(AbstractCsvWriter.java:146)
at org.supercsv.io.CsvListWriter.write(CsvListWriter.java:71)
at com.optrak.data.writing.CsvWriting$CsvFileWriter$$anonfun$write$1.apply(CsvWriting.scala:42)
有时候
java.lang.ArrayIndexOutOfBoundsException: -28
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:588)
at java.lang.StringBuilder.append(StringBuilder.java:214)
at org.supercsv.encoder.DefaultCsvEncoder.encode(DefaultCsvEncoder.java:81)
at org.supercsv.io.AbstractCsvWriter.escapeString(AbstractCsvWriter.java:102)
at org.supercsv.io.AbstractCsvWriter.writeRow(AbstractCsvWriter.java:196)
at org.supercsv.io.AbstractCsvWriter.writeRow(AbstractCsvWriter.java:146)
at org.supercsv.io.CsvListWriter.write(CsvListWriter.java:71)
at com.optrak.data.writing.CsvWriting$CsvFileWriter$$anonfun$write$1.apply(CsvWriting.scala:42)
具有不同的越界指数,通常为负数。显示的最后一个堆栈帧(省略完整输出)指向上述代码的第2行。我实际上在2个不同的specs2测试用例中使用代码。有时两个都失败了,有时一个如果我在两个测试用例中都得到异常,则超出范围的索引相同或相差一。什么可能导致这种看似随机的行为,我怎样才能找到原因并使其可预测?