My compiler is warning me that that an instance variable, a 2d int[][] array, might not have been initialised when I go to assign it.
I understand why the compiler might think that, because it is initialised in a double if statement. However the first if is on a boolean that is initialised to true, and the second if throws an exception on the else. I am confident of the logic of the program but the compiler obviously is not.
Does anyone have any tips for overcoming this kind of problem? I don't want to otherwise initialise the variable because it is meant to be final.
The variable of concern is the board variable. The below is part of a constructor for the object which contains the variable.
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
boolean first = true;
int lineCount = 0;
String line;
while ((line = br.readLine()) != null) {
String lineParts[] = line.split(" ");
if (first) {
if (lineParts.length == 2) {
this.xSize = Integer.parseInt(lineParts[0]);
this.ySize = Integer.parseInt(lineParts[1]);
board = new int[this.ySize][this.xSize];
first = false;
} else { throw new RuntimeException(); }
} else {
lineCount++;
if (lineParts.length == this.xSize) {
for (int i = 0; i < this.xSize; i++) {
board[lineCount][i] = Integer.parseInt(lineParts[i]);
}
} else throw new RuntimeException();
}
}
br.close();
if (lineCount != this.ySize) throw new RuntimeException();
}