Java ArrayIndexOutOfBound

懵懂的女人 提交于 2019-11-29 18:26:52
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.

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);



    }
});

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++;
}

problem in this string

if( c <= Playerlist.length) {

when

c = Playerlist.length

you will have a ARRAY_INDEX_OUT_OF_BOUNDS ERROR

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!