3

I am a beginner here in Java. So my problem is I am trying to hide all photo components of my window and make some others appear. But the problem is it always throws Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10 everytime I select an Item using JComboBox.

Here is the code block for the error:

for (cnt1=0; cnt1<10; cnt1++);
        {
            labels1[cnt1].setVisible(true);
        }

        for (cnt2=0; cnt2<10; cnt2++);
        {
            labels2[cnt2].setVisible(false);
        }


        for (cnt3=0; cnt1<10; cnt3++);
        {
            labels3[cnt3].setVisible(false);
        }

        for (cnt4=0; cnt4<10; cnt4++);
        {
            labels4[cnt4].setVisible(false);
        }

        for (cnt5=0; cnt5<10; cnt5++);
        {
            labels5[cnt5].setVisible(false);
        }

can you tell me what seems to be the problem here. I will post the whole code if you requests for it. Thank you in advance.

Edit: I have fixed the errors in my array, but this time,. the compiler gave me this error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

By the way, this is a snippet of the declaration my arrays:

String iconFiles1[] = {"Articuno.png", "Blastoise.png", "Charizard.png", "Kabutops.png", "Mewtwo.png", "Moltres.png", "Omastar.jpg", "Pikachu.jpg", "Venusaur.png", "Zapdos.png"};
String Jlabels1[] = {"Articuno", "Blastoise", "Charizard", "Kabutops", "Mewtwo", "Moltres", "Omastar", "Pikachu", "Venusaur", "Zapdos"};
ImageIcon icons1[] = new ImageIcon[iconFiles1.length];
JLabel labels1[] = new JLabel[Jlabels1.length];

and this is its assignment in the GUI:

for (int cnt1=0; cnt1<labels1.length; cnt1++)
        {
            labels1[cnt1].setVisible(true);
        }

am I doing it right? Answers are appreciated. Again. Thank you.

6
  • Answer will depend on what is there in labelsx[] arrays. Commented Jul 7, 2013 at 9:17
  • Shouldn't this be cnt3 for (cnt3=0; cnt1<10; cnt3++);? Commented Jul 7, 2013 at 9:17
  • 1
    The semicolon in the first line looks wrong. What is in the labels*-Arrays? Commented Jul 7, 2013 at 9:18
  • for better help sooner post an SSCCE, short, runnable, compilable, generating a.m. exception Commented Jul 7, 2013 at 9:18
  • seems u have labels1 upto [9] ... but u r trying to access labels1[10]... ur cnt1 = 10 after the first for loop Commented Jul 7, 2013 at 9:20

4 Answers 4

2

You appear to have incorrect variables in at least one of your for loops (as several other posters have already pointed out).

Nonetheless, the reason you're getting an ArrayIndexOutOfBoundsException is because you have semicolons after all of your for loops, thereby making them do nothing but increment your counter variables. The variables that you're incrementing in your for loops are not locally declared in the for loops themselves, so their scope is whatever function you're currently in (aka they exist outside of the for loops). So when the loops finish incrementing, your counters will be too large (10 in this case)---> hence the ArrayIndexOutOfBoundsException: 10.

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

2 Comments

@SageTheGreat This completely answers your question.
@SageTheGreat Also, since you're new, you should take the tour.
0

First, you have a semicolon after every for statement, which basically tells the compiler to loop without executing code.

for (cnt1=0; cnt1<10; cnt1++);
{
    labels1[cnt1].setVisible(true);
}

Must become

for (cnt1=0; cnt1<10; cnt1++)
{
    labels1[cnt1].setVisible(true);
}

And so on for every loop.

Second: I think this

for(cnt3=0; cnt1<10; cnt3++)

Should become

for(cnt3=0; cnt3<10; cnt3++)

Comments

0
for (cnt1=0; cnt1<10; cnt1++);
  {
     labels1[cnt1].setVisible(true);
  }

You have put semi-colon in first statement. So your loop is iterating 10 times without doing nothing. Value cnt1 when we get out of loop is 11.

Then you try to set 11th element of array labels1[cnt1].setVisible(true);

As you may not be having 11 elements in array you are getting IndexOutofBound. Do like this :

for (cnt1=0; cnt1<10; cnt1++)
  {
     labels1[cnt1].setVisible(true);
  }

Comments

0

I see three things that don't seem to work in your code:

  • for (cnt1=0; cnt1<10; cnt1++); and all other lines should not end with a semi-colon
  • for (cnt3=0; cnt1<10; cnt3++); the reference to cnt1 in this line should probably be to cnt3
  • In general, you should reference the size of your Array, such as:

    for (int i = 0; i < labels.length; i++)

This way you are sure you won't reference any value out of the Array's bounds.

Comments

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.