java无法填充数组
你好,我有一个公共整数数组,我想在数组中存储 1 或 2,但我收到错误 NullPointerException 我在做什么,这个
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 error");
}
repaint();
}
错误是在我填充名为的变量时发生的,我不知道为什么会这样,谢谢非常
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 error");
}
repaint();
}
the error is when im filling the variable called which i have no idea why is that,thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要先分配数组,然后才能访问其元素:
其中
n
是所需的数组大小。如果您事先不知道数组的大小,则可以按原样保留变量声明,并稍后进行分配(但在尝试使用数组之前):
You need to allocate the array before you can access its elements:
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):