为什么我将null作为数组的值? [英] Why am I getting null as a value of array?
问题描述
我有一个Hra1课程,它定义了游戏规则(游戏= hra)。问题是,我得到一个空值,例如poleMinci == null,尽管数组poleMinci是在构造函数中创建的。换句话说,玩家的移动方法总是返回false。
I have a class Hra1, which defines the rules of a game (game=hra). The problem is, that I am getting a null value, e.g. poleMinci==null, despite the array poleMinci is created in the constructor. In other words, the player's move method always returns false.
构造函数:
public Hra1()
{
Mince [] poleMinci = new Mince[20];
poleMinci[0] = new Mince("stříbrná", "coin.png");
poleMinci[3] = new Mince("stříbrná", "coin.png");
poleMinci[4] = new Mince("zlatá", "coin_gold.png");
poleMinci[8] = new Mince("stříbrná", "coin.png");
poleMinci[10] = new Mince("stříbrná", "coin.png");
poleMinci[12] = new Mince("stříbrná", "coin.png");
}
玩家的移动方式:
public boolean tahHrace(Tah tah){
if (poleMinci != null){
int odkud = tah.getZPozice();
int kam = tah.getNaPozici();
boolean kamPrazdne;
if (poleMinci [kam] != null)
kamPrazdne = false;
else
kamPrazdne = true;
if (kam > odkud && poleMinci [odkud] != null && kamPrazdne == true){
poleMinci [kam] = poleMinci [odkud];
poleMinci [odkud] = null;
System.out.println("hráč táhl z pozice "+tah.getZPozice()
+ " na pozici "+tah.getNaPozici());
return true;
}
else
return false;
}
else
return false;
}
推荐答案
您正在隐藏变量:
public Hra1()
{
// the following variable's *scope* is inside of this constructor only
// outside of the constructor, the local variable below doesn't exist.
Mince [] poleMinci = new Mince[20];
poleMinci[0] = new Mince("stříbrná", "coin.png");
poleMinci[3] = new Mince("stříbrná", "coin.png");
poleMinci[4] = new Mince("zlatá", "coin_gold.png");
poleMinci[8] = new Mince("stříbrná", "coin.png");
poleMinci[10] = new Mince("stříbrná", "coin.png");
poleMinci[12] = new Mince("stříbrná", "coin.png");
}
在该构造函数中,由于poleMinci是在构造函数内部声明的,因此它是可见的仅在构造函数内部。如果类中有一个同名变量,它将为null。要解决此问题,请不要在本地重新声明变量。执行:
In that constructor, since poleMinci was declared inside of the constructor, it is visible only inside of the constructor. If you have a variable of the same name in the class it will be null. To fix this, don't re-declare the variable locally. Do:
public Hra1()
{
// Mince [] poleMinci = new Mince[20]; // **** not this ****
poleMinci = new Mince[20]; // **** but this. note the difference? ****
poleMinci[0] = new Mince("stříbrná", "coin.png");
poleMinci[3] = new Mince("stříbrná", "coin.png");
poleMinci[4] = new Mince("zlatá", "coin_gold.png");
poleMinci[8] = new Mince("stříbrná", "coin.png");
poleMinci[10] = new Mince("stříbrná", "coin.png");
poleMinci[12] = new Mince("stříbrná", "coin.png");
}
有关此问题的更多信息,请查看变量阴影。大多数IDE都会警告您可能正在执行此操作,或者设置允许它们执行此操作。我使用Eclipse并设置了我的IDE来警告我。您可能也希望这样做。
For more on this problem, check out Variable Shadowing. Most IDE's either will warn you that you might be doing this, or have a setting that will allow them to do this. I use Eclipse and have set my IDE to warn me. You might wish to do this too.
这篇关于为什么我将null作为数组的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!