I am trying to create a map in the shape of grid 4 columns X 5 Rows and connect each rooms together. Below code compiles but when I try to run it it comes up with the error. Could anyone please tell me where its gone wrong? Thanks.
public static final int NUM_ROOMS = 20;
public static final int NUM_COLS = 4;
private ArrayList<Room> myRooms;
public Map(){
int row =0;
ArrayList<Room> myRooms = new ArrayList<Room>();
while (row<NUM_COLS){
int i =0;
i = row % NUM_COLS;
while(i+1<NUM_COLS){
while (i+1 <(row+1)% NUM_COLS){
Room r1 = myRooms.get(i);
Room r2 = myRooms.get(i+1);
r1.connectTo(r2);
r2.connectTo(r1);
i++;
}
row++;
}
int col = 0;
while (col<NUM_COLS){
i = col;
while (i+4<NUM_ROOMS){
i = i+NUM_COLS;
}
col++;
}
}
}
The issue is that the
myRoomslist is empty. So when you try to get an element out of the list, you get an exception. You have to use something liketo populate the list first.