0

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?

5
  • Please show us which is line 64 in file setevelger.java Commented Feb 6, 2012 at 21:27
  • 2
    how can j == 10 ever be true?? Commented Feb 6, 2012 at 21:30
  • This isn't really answering your question, but it'll help. you have 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 loops sete and rad are not needed, you can just use i and j. "rad " +(i+1)+", Sete "+(j+1)); Then you don't need the parts after antallSeter++; Commented Feb 6, 2012 at 21:31
  • could you show us what's on setevelger.java line 64 and 22 ? Commented Feb 6, 2012 at 21:32
  • Pleas don't start your class name with a lower case (new knapp(...)). This does not fit the java naming convention and it prevents confusion. ;) Commented Feb 6, 2012 at 21:42

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

why you test for j == 10 ? some micro - optimalization :) ?
Im testing because I want each seat to have a seat number, and each row is supposed to have 10 seats, so when j==10 so that next row starts from 0 again:)
you testing is included in for cycle condition evaluation phase (int j=0;j<10;j++) - there is no way how j == 10 , do not need sete variable, use j instead
1

In your code, you have this reset code:

if (j==10) {
    sete=0;
} else {
    sete++;
}

However, it can never be that j == 10, so sete never gets reset to 0. I don't know if that's what's causing the exception, but it's definitely a problem with your code. The condition should probably be if (j==9) { ...

Comments

1

Both exceptions occur because the penultimate line:

seter[antallSeter].addActionListener(lytteren);

is outside of the double for loop, where antallSeter will have been incremented beyond the number of existing (1st exception) or populated (2nd exception) indices of seter.

If I understand correctly, and you'd like to add an event listener to every button, you could change to:

// ..

    if(j == 10) {
        sete=0;
    } else {              
        sete++; 
    }

    seter[antallSeter].addActionListener(new Knappelytter());

}
rad++;
// etc ..

Comments

0

After the last loop, the value of antallSeter is 100. which is an index out of bounds in the line:

seter[antallSeter].addActionListener(lytteren);

You should probably move the lines:

Knappelytter lytteren = new Knappelytter();
seter[antallSeter].addActionListener(lytteren);

Inside the outer loop.

Comments

0

Initialise seter with 101 size.

public knapp seter[]=new knapp[101];

1 Comment

A array with 100 buttons won't need a size of 101 elements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.