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?
j == 10ever be true??if(j==10)in your loop that states..;j<10;..which means that j will never equal 10. Also, unless they're used outside of your loopsseteandradare not needed, you can just useiandj."rad " +(i+1)+", Sete "+(j+1));Then you don't need the parts afterantallSeter++;