我想從文件中讀取數字的行。代碼如下,但IDE顯示NullPointerException運行時異常。不知道我做錯了什麼。for循環在java中的二維數組
//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();
}
}
哪條線顯示nullpointerexception? – Kshitij
粘貼錯誤日誌,請 –
我寧願給我們一個簡單的'List'列表',其索引是行號和值是整數。 – 2012-06-26 05:05:29