Monday, 9 September 2013

NullPointerException while trying to initialize a 2d array

NullPointerException while trying to initialize a 2d array

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());
}

No comments:

Post a Comment