我的問題是嘗試讀取包含不齊整數組的文件。我想我幾乎在那裏,但我不斷收到空指針異常錯誤。歡迎任何幫助或建議。這是我的代碼。將文件讀入不規則數組
import java.io.*;
import java.util.Scanner;
import java.util.*;
public class Calories2{
public static void fileReaderMethod(String fileName) throws FileNotFoundException, IOException {// This method reads the file and inputs the measurements
Scanner input ;
try{ // into an array of type int.
input = new Scanner (new File(fileName));
}
catch (FileNotFoundException e){
System.out.println("File Does Not Exist");
System.out.println("Choose another file");
Scanner in = new Scanner(System.in);
fileName = in.next();
input = new Scanner (new File(fileName));
}
FileReader fr = new FileReader(fileName);
BufferedReader textReader = new BufferedReader(fr);
int row=0;
String currentRow;
while((currentRow = textReader.readLine())!= null){
rows++;
}
fr = new FileReader(fileName);
textReader = new BufferedReader(fr);
String [] columns = textReader.readLine().split(" ");
int [][] caloriesOfTheWeekArray = new int [7][];
fr = new FileReader(fileName);
textReader = new BufferedReader(fr);
for(int i = 0; i < caloriesOfTheWeekArray.length; i++){
String columnArray [] = textReader.readLine().split(" ");
for (int j = 0; j < columns.length; j++){
caloriesOfTheWeekArray[i][j] = Integer.parseInt(columnArray[j]);
}
}
}
public static void main (String [] args)throws FileNotFoundException, IOException {
fileReaderMethod("Calories.txt");
}
}
這是我得到的錯誤。
Exception in thread "main" java.lang.NullPointerException
at Calories2.fileReaderMethod(Calories2.java:36)
at Calories2.main(Calories2.java:41)
線36
caloriesOfTheWeekArray[i][j] = Integer.parseInt(columnArray[j]);
和41是在那裏我調用讀取文件
文件我想讀
200 10000
450 845 1200 800
800
400 1500 1800 200
500 1000
700 1400 170
675 400 100 400 300
發佈您的錯誤跟蹤以及。 –
你的意思是我得到的錯誤? –
@Micheal yupp,你得到的錯誤。 –