【发布时间】:2012-06-27 09:25:51
【问题描述】:
我想从文件中读取数字行。代码如下,但 IDE 显示 NullPointerException 运行时异常。不知道我做错了什么。
//reading the contents of the file into an array
public static void readAndStoreNumbers() {
//initialising the new object
arr = new int[15][];
try {
//create file reader
File f = new File("E:\\Eclipse Projects\\triangle.txt");
BufferedReader br = new BufferedReader(new FileReader(f));
//read from file
String nums;
int index = 0;
while ((nums = br.readLine()) != null) {
String[] numbers = nums.split(" ");
//store the numbers into 'arr' after converting into integers
for (int i = 0; i < arr[index].length; i++) {
arr[index][i] = Integer.parseInt(numbers[i]);
}
index++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
哪一行显示空指针异常?
-
我宁愿我们一个简单的
List或List,索引是行号,值是整数。
标签: java arrays multidimensional-array integer