NullPointerException异常试图初始化一个二维数组 [英] NullPointerException while trying to initialize a 2d array
问题描述
我得到一个NullPointerException异常试图从文本文件添加值二维数组。第一2值确定的行和列。任何想法是把它扔。忽略异常处理和打印报表。我试图让初始化,然后会回去和牛肉它一点的阵列。
I am getting a nullPointerException trying to add values from a text file to a 2d array. The first 2 values determine the rows and columns. Any ideas what is throwing it. Ignore the exception handling, and the print statements. I am trying to get the array initialized then will go back and beef it up a bit.
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
西:5
0.0
异常的线程主显示java.lang.NullPointerException
OUTPUT Reading in file: sampleMap2.txt Row : 5 Col : 5 0.0 Exception in thread "main" java.lang.NullPointerException
推荐答案
你期望什么样的价值在这里看到;
What value do you expect to see here;
baseMap = new double[row][col];
System.out.println(baseMap[2][4]);
怎么样,如果排== 1?
How about if row == 1?
还有什么,如果没有更多的数据为:
Also what if there is no more data at:
baseMap[i][j] = Double.parseDouble(inputFile.next());
也许你只是没有足够的数据。
Maybe you just don't have enough data.
这篇关于NullPointerException异常试图初始化一个二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!