![](/img/trans.png)
[英]NullPointerException while passing 2d array to the constructor
[英]NullPointerException while trying to initialize a 2d array
我收到一個nullPointerException,試圖將值從文本文件添加到2d數組。 前兩個值確定行和列。 任何想法都在拋出它。 忽略異常處理和打印語句。 我正在嘗試初始化數組,然后將其返回並對其進行增強。
public Help(String filename) throws FileNotFoundException,
InvalidFileFormatException {
this.filename = filename;
System.out.println("Reading in file: " + filename);
String number = "";
int row = 0;
int col = 0;
int count = 0;
try {
Scanner inputFile = new Scanner(new File(filename));
while (inputFile.hasNextInt()) {
row = Integer.parseInt(inputFile.next());
col = Integer.parseInt(inputFile.next());
System.out.println("Row : " + row);
System.out.println("Col : " + col);
baseMap = new double[row][col];
System.out.println(baseMap[2][4]);
for (int i = 0; i < baseMap.length; i++){
for (int j = 0; j < baseMap[i].length; j++){
baseMap[i][j] = Double.parseDouble(inputFile.next());
}
}
}
System.out.println(baseMap[2][4]);
} catch (Exception e) {
System.out.println(e.toString());
}
輸出讀入文件:sampleMap2.txt行:5 Col:5 0.0線程“主”中的異常java.lang.NullPointerException
您希望在這里看到什么價值?
baseMap = new double[row][col];
System.out.println(baseMap[2][4]);
如果row == 1呢?
如果沒有更多數據,該怎么辦:
baseMap[i][j] = Double.parseDouble(inputFile.next());
也許您只是沒有足夠的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.