java 数组 null值_java-为什么我将null作为数组的值?

您正在阴影变量:

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.要解决此问题,请不要在本地重新声明变量.做:

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

}

有关此问题的更多信息,请签出Variable Shadowing.大多数IDE都会警告您可能正在执行此操作,或者有一个允许他们执行此操作的设置.我使用Eclipse并将IDE设置为警告我.您可能也希望这样做.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值