I have the class Mappa and subclass MappaFermi with the array attribute Sector sector, each of the cells of the array is supposed to have a name, in the code of the class constructor i haven't had any problem naming them (all tests were successfull), however in the subclass constructor i get ArrayIndexOutOfBoundsException: 14 when trying to run a NotNull assertion. I set the array as protected so i can use it in the subclass.
public class Mappa {
private Name mappaName;
protected Sector [][] sector;
private int Matrix [][];
private static final int X=23;
private static final int Y=14;
public Mappa (Name mappaName){
this.mappaName=mappaName;
sector = new Settore[X][Y];
for (int i=0; i < X; i++){
for (int j=0; j<Y; j++) {
sector[i][j] = new Settore (i,j);
}
}
Matrix = new int[23][14];
if(mappaName==Name.FERMI){
sector[10][8]=new Alieni(10,8);
sector[10][9]=new Umani(10,9);
}
if(mappaName==Name.GALILEI||mappaName==Name.GALVANI){
sector[10][5]=new Alieni(10,5);//i have run all tests and it was successful
sector[10][7]=new Umani(10,7);
}
}
}
public class MappaFermi extends Mappa {
public MappaFermi() {
super(null);
new Mappa(Name.FERMI);
setMatrix(new int[][]{
{0,0,0,0,0,0,0,0,0,3,0,0,0,3,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,2,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,2,1,2,1,2,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,2,2,2,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,3,0,1,0,3,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,2,1,0,1,0,2,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,2,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,1,0,0,0,1,2,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,1,1,1,1,1,0,2,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,2,1,0,1,0,2,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,2,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}});
for (int i=0; i < 14; i++){
for (int j=0; j<23; j++){
if (getMatrix()[i][j]==1){
sector[i][j]=new Sicuro(i,j);//this is where i get the error, if i only write `new Sicuro(i,j);` it runs successfully but i'm not storing the value in the cell of the array
}
else {
if (getMatrix()[i][j]==2){
sector[i][j]=new Pericoloso(i,j);
}
else {
if (getMatrix()[i][j]==3){
sector[i][j]=new Scialuppa(i,j);
}
}
}
}
}
}
}
public class MappaFermiTest {
@Test
public void testMappaFermi() {
Mappa mappa = new MappaFermi();
assertNotNull(mappa);
}
}
sector has dimensions
[23][14]
but you're trying to initialize it with an iteration[i in (0..14)]``[j in (0..23)]
. You should either initialize it like this:sector = new Settore[Y][X];
or inverse X and Y values.