我收到一个nullPointerException,试图将文本文件中的值添加到二维数组中。前2个值确定行和列。是什么想法扔掉它。忽略异常处理和print语句。我试图让数组初始化,然后回去加强一点。
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 线程“main”中的异常java.lang.NullPointerException
答案 0 :(得分:2)
您希望在这里看到什么价值;
baseMap = new double[row][col];
System.out.println(baseMap[2][4]);
如果row == 1怎么样?
如果没有更多数据,那该怎么办:
baseMap[i][j] = Double.parseDouble(inputFile.next());
也许你的数据不够。