开发者

cant fill array java

开发者 https://www.devze.com 2023-04-06 23:06 出处:网络
hi ive got a public array of ints and i want to store 1 or 2 in the array but im getting the error NullPointerException what im doing is this

hi ive got a public array of ints and i want to store 1 or 2 in the array but im getting the error NullPointerException what im doing is this

    public int[] which;
    public int gotIt;

public void Check()
{


    int cont = 0;

   System.out.println(intento[0]);

        for(int j = 0;j <= spaces;++j)
        {
             if(tries[0] == words[numRandom][j])
            {
                which[gotIt] = j;//im getting the error here
                gotIt++;
            }
            else
            {
                 cont++;
            }
       }
        if(Contador == espacios+1)
        {
            Errors++;
            System.out.println("There was an err开发者_如何学运维or");
        }

        repaint();
}

the error is when im filling the variable called which i have no idea why is that,thank you very much


You need to allocate the array before you can access its elements:

public int[] which = new int[n];

where n is the desired size of the array.

If you don't know the size of the array upfront, you can leave the variable declaration as-is, and do the allocation later on (but before you attempt to use the array):

which = new int[n];
0

精彩评论

暂无评论...
验证码 换一张
取 消