java.lang.ArrayIndexOutOfBoundsException:长度=1;尝试读取 csv 时 index=1
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 when trying to read csv
我在尝试读取 csv 文件时遇到问题,错误显示如下:java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
。我尝试在 Whosebug 中搜索,错误与上述相同。
代码:
private void readCSVFile(String csvNameFile) {
try {
File readFolderCSV = new File(Environment.getExternalStorageDirectory() + "/Download/" + csvNameFile);
CSVReader csvReader = new CSVReader(new FileReader(readFolderCSV.getAbsoluteFile()));
String[] nextLine;
while((nextLine = csvReader.readNext()) != null) {
Log.e("TAG", nextLine[0] + nextLine[1] + nextLine[2] + nextLine[3] + nextLine[4]);
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MasterUoM.this, "The specified file was not found", Toast.LENGTH_SHORT).show();
}
}
我的CSV文件:
您的问题在这里:
while((nextLine = csvReader.readNext()) != null) {
Log.e("TAG", nextLine[0] + nextLine[1] + nextLine[2] + nextLine[3] + nextLine[4]);
}
您的日志记录长度不安全。您有一行 nextLine[1]
不存在。那是因为 csvreader 没有 return 单元格,而是线条。
您需要使用拆分方法拆分行。 Java split string to array 并再次检查 table 的长度是否足够。
3,4 和 5 小区访问会给您带来同样的麻烦。
您似乎正在尝试获取列[2] 和列[3] 以及列[4],但 csv 没有 5 列,
Log.e("TAG", nextLine[0] + nextLine[1] + nextLine[2] + nextLine[3] + nextLine[4]);
打印nextLine.length()即可查看。
我在尝试读取 csv 文件时遇到问题,错误显示如下:java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
。我尝试在 Whosebug 中搜索,错误与上述相同。
代码:
private void readCSVFile(String csvNameFile) {
try {
File readFolderCSV = new File(Environment.getExternalStorageDirectory() + "/Download/" + csvNameFile);
CSVReader csvReader = new CSVReader(new FileReader(readFolderCSV.getAbsoluteFile()));
String[] nextLine;
while((nextLine = csvReader.readNext()) != null) {
Log.e("TAG", nextLine[0] + nextLine[1] + nextLine[2] + nextLine[3] + nextLine[4]);
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MasterUoM.this, "The specified file was not found", Toast.LENGTH_SHORT).show();
}
}
我的CSV文件:
您的问题在这里:
while((nextLine = csvReader.readNext()) != null) {
Log.e("TAG", nextLine[0] + nextLine[1] + nextLine[2] + nextLine[3] + nextLine[4]);
}
您的日志记录长度不安全。您有一行 nextLine[1]
不存在。那是因为 csvreader 没有 return 单元格,而是线条。
您需要使用拆分方法拆分行。 Java split string to array 并再次检查 table 的长度是否足够。
3,4 和 5 小区访问会给您带来同样的麻烦。
您似乎正在尝试获取列[2] 和列[3] 以及列[4],但 csv 没有 5 列,
Log.e("TAG", nextLine[0] + nextLine[1] + nextLine[2] + nextLine[3] + nextLine[4]);
打印nextLine.length()即可查看。