[英]trouble iterating through array containing objects in java
I've been looking for a while and haven't found an answer to this question in the specific scenario that I have.我一直在寻找一段时间,并没有在我所拥有的特定场景中找到这个问题的答案。
I have an assignment in which I need to create a program that takes the random output of 5 dice and determines possible scoring values for the game Yahtzee.我有一个任务,我需要创建一个程序,该程序采用 5 个骰子的随机 output 并确定游戏 Yahtzee 的可能得分值。
Right now I have a class that contains the dice, which I need to use to create an array of 5 later.现在我有一个包含骰子的 class,我需要用它来创建一个 5 的数组。
package Assign1;
public class Dice {
int value;
public Dice(){
value = -1;
}
public Dice(int num){
value = num;
}
public void roll(){
int num = RandomNumber.getRandomNumber(1, 6);
value = num;
}
public int getValue(){
return value;
}
}
In my next file, I have to create that array, and for each object in the array, run the roll()
method, which should change the value of the object to a random number.在我的下一个文件中,我必须创建该数组,并且对于数组中的每个 object,运行roll()
方法,该方法应该将 object 的值更改为随机数。 Right now, I can't run that code.现在,我无法运行该代码。
package Assign1;
public class Yahtzee {
Dice[] dice;
public Yahtzee(){
dice = new Dice[5];
for (int i = 0; i < dice.length; i++){
dice[i].roll();
}
}
public static void main(String[] args) {
Yahtzee var = new Yahtzee();
int test = var.dice[0].getValue();
System.out.println(test);
}
}
The bottom section is me simply trying to see if my code will work correctly.底部是我只是想看看我的代码是否能正常工作。
I am trying to iterate through the array of dice objects and access the index using i
, which will know the object and roll the value.我正在尝试遍历 dice 对象数组并使用i
访问索引,这将知道 object 并滚动该值。
I get the following error:我收到以下错误:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Assign1.Dice.roll()" because "Assign1.Yahtzee.dice[0]" is null线程“主”java.lang.NullPointerException 中的异常:无法调用“Assign1.Dice.roll()”,因为“Assign1.Yahtzee.dice[0]”是 null
I have tried multiple ways of performing this action to no avail, so my last resort is asking here.我尝试了多种执行此操作的方法均无济于事,所以我最后的手段是在这里询问。
I'm used to programming with Python, so java is a much different language for me, and a lot of the things I want to execute require a far different approach.我习惯于使用 Python 进行编程,因此 java 对我来说是一种截然不同的语言,而且我想要执行的很多事情都需要一种截然不同的方法。
Any help would be greatly appreciated on what I am doing wrong here and any issues I may run into in the future because of how I code due to python.对于我在这里做错的事情以及由于 python 导致我如何编码而可能遇到的任何问题,我们将不胜感激。
Thanks谢谢
tl;dr tl;博士
receive the following error while trying to iterate through an object array and access the object and its methods: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Assign1.Dice.roll()" because "Assign1.Yahtzee.dice[0]" is null receive the following error while trying to iterate through an object array and access the object and its methods: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Assign1.Dice.roll()" because "Assign1.Yahtzee.dice [0]" 是 null
This这个
dice = new Dice[5];
creates storage for 5 Dice but does not instantiate them, they are still null为 5 个 Dice 创建存储但不实例化它们,它们仍然是 null
You need to do你需要做
for (...)
newdice = new Dice();
then you need to call roll
on this new Object那么你需要在这个新的roll
上打电话
newdice.roll();
then you can add it to your array然后你可以将它添加到你的数组中
dice[i] = newdice;
Coming together it would be聚在一起就会
public Yahtzee(){
dice = new Dice[5];
for (int i = 0; i < dice.length; i++){
Dice newdice = new Dice();
newdice.roll();
dice[i] = newdice;
}
}
Of course The default constructor could be modified to actually call roll
, then you could get rid of the other constructor当然可以将默认构造函数修改为实际调用roll
,然后您可以摆脱其他构造函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.