我试图跳过我的.csv文件的第一行,这是我的第一个if语句应该解决的问题,然后遍历.csv文件并创建新的Call对象并将它们添加到名为callList的数组中。它不是读取第一行,而是只读取第一行,然后给我一个NullPointerException
。任何的想法?为什么会这样?
public static void parseCalls(String fileName) {
int firstRow = 1;
int counter = firstRow;
String line = "";
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
while ((line = br.readLine()) != null) {
if(counter == firstRow) { //should skip first row
br.readLine();
}
// use comma as separator
String[] splitCalls = line.split(",");
callList.add(new Call(splitCalls[6], splitCalls[9], splitCalls[25], splitCalls[17]));
counter++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
parseCalls("test.csv");
}
我的堆栈跟踪是:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at CallAnalyzer.parseCalls(CallAnalyzer.java:95)
at CallAnalyzer.main(CallAnalyzer.java:323)
当我致电parseCalls()
时,每次发生错误。
答案 0 :(得分:0)
首先,像Boris the Spider在评论中说你可以使用Files.lines(...).skip(1).map(line -> line.split(","))
但是在你的第一个代码中如果你读了这行但是没有把它分配给你的行变量。因此,稍后在代码中尝试拆分包含第一行的变量行的内容。您没有提供堆栈跟踪,但我在NullPointerException
上发出了splitCalls[6]
个问题,而您的splitCalls没有第6个元素,因为您拆分了错误的行。如果您想继续使用您的代码,请执行以下操作:
public static void parseCalls(String fileName) {
int firstRow = 1;
int counter = firstRow;
String line = "";
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
while ((line = br.readLine()) != null) {
if(counter == firstRow) { //should skip first row
counter++;
continue;
}
// use comma as separator
String[] splitCalls = line.split(",");
callList.add(new Call(splitCalls[6], splitCalls[9], splitCalls[25], splitCalls[17]));
counter++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
或者更好:
public static void parseCalls(String fileName) {
boolean firstRow = true;
String line = "";
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
while ((line = br.readLine()) != null) {
if(firstRow) { //should skip first row
firstRow = false;
continue;
}
// use comma as separator
String[] splitCalls = line.split(",");
callList.add(new Call(splitCalls[6], splitCalls[9], splitCalls[25], splitCalls[17]));
}
} catch (IOException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
if(counter == firstRow) { //should skip first row
br.readLine();
}
Here you are not assigning the result of readLine()
to the variable line
. But it would be even better to bring this out of the loop, and read a line before going into the loop. That way you don't have to check for the first line every time a line is read.
String[] splitCalls = line.split(",");
callList.add(new Call(splitCalls[6], splitCalls[9], splitCalls[25], splitCalls[17]));
Here you should really check the length of splitCalls[]
first before accessing any elements. The content of a csv file should never be trusted, and some lines could have only 3 columns for example, which throws ArrayIndexOutOfBoundsException
(but the reason you get this now is because line
still contains the first line of your file and probably contains less items).
So you would have something like:
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
br.readLine();
while ((line = br.readLine()) != null) {
String[] splitCalls = line.split(",");
// check length of array here, if ok:
callList.add(new Call(splitCalls[6], splitCalls[9], splitCalls[25], splitCalls[17]));
}
} catch (IOException e) {
e.printStackTrace();
}
答案 2 :(得分:0)
致电
line = br.readLine();
在if分支中,稍后在分割之前测试line!= null。否则第一行仍然存储在行中。
它在我的系统上只读了一行。
我猜你的第一行是一些注释或标题,不包含足够的逗号来分割字符串,因此splitCalls [6]触发了exeption,当然,不会再读取任何行。