I get this exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at Vindu.<init>(setevelger.java:64)
at setevelger.main(setevelger.java:22)
when I’m running this code:
public knapp seter[]=new knapp[100]; //knapp means button
int rad=0; //rows
int sete=0; //seats
int antallSeter=0; //number of seats
for (int i=0;i<10;i++){
for (int j=0;j<10;j++){
seter[antallSeter]= new knapp("Rad "+(rad+1)+", Sete "+(sete+1));
seter[antallSeter].setBackground(Color.GREEN);
add(seter[antallSeter]);
antallSeter++;
if(j==10){
sete=0;
}else{
sete++;
}
}
rad++;
}
//creates an eventlistener
Knappelytter lytteren = new Knappelytter();
seter[antallSeter].addActionListener(lytteren);
pack();
and if I do this:
public knapp seter[]=new knapp[120]; //knapp means button
I get this error:
Exception in thread "main" java.lang.NullPointerException
at Vindu.<init>(setevelger.java:64)
at setevelger.main(setevelger.java:22)
both errors come at runtime when the window is being created.
So, the code is supposed to create 100 buttons and store them in an array, and each button is to have a row and seat number.
I’m stuck, I have no idea where to look anymore..
Should I perhaps use an arraylist?
In your for cycles antalSetter is increased to 100 in last cycle – first exception, if you increase array length to 120 – you are accesing element at index 100 that is null.