我正在尝试在 csv 文件中创建一个行数组。
示例 csv:
12,13,14,15
13,14,15,16
11,12,13,14
现在我希望我的数组包含 3 个字符串。
我懂了:
public static String[] postcodeRows;
public static void main(String[] arg) throws Exception
{
//read the csv file
BufferedReader CSVFile = new BufferedReader(new FileReader(
"C:\\Users\\randy\\Documents\\postcode csv\\exports\\all-groups.csv"));
//count where we are in the csv file
int csvLine = 0;
postcodeRows[0] = CSVFile.readLine(); // Insert the first line.
// The while checks to see if the data is null. If it is, we've hit the end of the file. If not, process the data
while (postcodeRows[csvLine] != null)
{
csvLine++;
postcodeRows[csvLine] = CSVFile.readLine();
}
// Close the file once all data has been read.
CSVFile.close();
}
现在我明白了:
Exception in thread "main" java.lang.NullPointerException
at postcodeCheckup.postcodePanel.main(postcodePanel.java:43)
为什么有一个NullPointerException
,我该如何防止这种情况?变量 can't be null
,由while
循环检查。