Java ArrayIndexOutOfBound

后端 未结 4 686
感情败类
感情败类 2020-12-22 13:43

I am currently working on a student project and everytime I get this error: ArrayIndexOutOfBoundsException: 7. Can someone see where it occurs and how I can fix

相关标签:
4条回答
  • 2020-12-22 14:22

    problem in this string

    if( c <= Playerlist.length) {

    when

    c = Playerlist.length

    you will have a ARRAY_INDEX_OUT_OF_BOUNDS ERROR

    0 讨论(0)
  • 2020-12-22 14:33

    Try it now, you have bad comparison, in loop:

       public void actionPerformed(ActionEvent e) {
            if(c >= Playerlist.length)
            {
                if(c >= wuerfelsummen.length)
                {
                    c = 0;
                }
            }
            System.out.println(c);
            if(wuerfelsummen[c] == null)
            {
                c++;
            }
    
            System.out.println(c);
    
    
            wuerfelsummen[c].setText(lbl_summe.getText());
            lbl_summe.setText("0");
            lbl_summe.setForeground(Color.black);
            btnWerfeln.setEnabled(true);
            pot.setCurrentPlayer(Playerlist[c]);
            if(c >= Playerlist.length)
            {
                c = 0;
            }
            else
            {
                c++;
    
                //ARRAY_INDEX_OUT_OF_BOUNDS ERROR !!!!!!!!!!!!!!!!!!!!
                while(wuerfelsummen[c] == null)
                {
                    if( c < Playerlist.length-1)
                    {
                        c++;
                    }
                    else
                    {
                        c = 0;
                        //ermittleGewinner(wuerfelsummen);
                    }
    
                }
            }
            System.out.println(c);
            //ermittleGewinner(wuerfelsummen);
    
    
    
        }
    });
    
    0 讨论(0)
  • 2020-12-22 14:36

    Currently you are incrementing the index at the end of the array, causing the program to go outside the bounds of the array. Therefore you should change...

    if(c <= Playerlist.length)
    {
        c++;
    }
    

    ...to...

    if(c < Playerlist.length)
    {
        c++;
    }
    
    0 讨论(0)
  • 2020-12-22 14:42
    if(c >= Playerlist.length)
            {
                c = 0;
            }
            else
            {
                c++;
    
                //ARRAY_INDEX_OUT_OF_BOUNDS ERROR !!!!!!!!!!!!!!!!!!!!
                while(wuerfelsummen[c] == null)
    

    You are first checking if c is at most the last index of the array, and afterwards increment it by 1, possibly pushing it over that limit.

    0 讨论(0)
提交回复
热议问题