Random 返回JLabel时发生NullPointerException
我似乎有一个nullPointerException没有任何原因,请您检查一下我的代码并告诉我您的意见好吗? 这是我在另一个类上调用的类和构造函数,以便从链接列表中随机获得标签(使用随机排列,这也是随机的)。 这是你的电话号码Random 返回JLabel时发生NullPointerException,random,nullpointerexception,shuffle,Random,Nullpointerexception,Shuffle,我似乎有一个nullPointerException没有任何原因,请您检查一下我的代码并告诉我您的意见好吗? 这是我在另一个类上调用的类和构造函数,以便从链接列表中随机获得标签(使用随机排列,这也是随机的)。 这是你的电话号码 public class RandomHeuristic { GameInterface game; JLabel randomLabel; public JLabel RandomHeuristic() { randomLabe
public class RandomHeuristic {
GameInterface game;
JLabel randomLabel;
public JLabel RandomHeuristic() {
randomLabel = (JLabel) game.labels.getFirst();
int counter = 0;
do {
Collections.shuffle(game.labels);
randomLabel = (JLabel) game.labels.getFirst();
counter++;
if (counter == 100) {
break;
}
/*
* Debugging
* System.out.println(randomLabel.getText());
*/
} while (randomLabel != null && game.isLegalMove(randomLabel) == false);
//Retrieves and removes the head (first element) of this list.
if(randomLabel == null){
RandomHeuristic();
}
//game.labels.remove(randomLabel);
return randomLabel;
}
}
这里是我调用构造函数的地方,playHeuristicMove()需要一个JLabel,我在调试时检查过它,它工作正常,但调用它时仍然会出现null指针异常。randomHeuristicOne是在同一个类上创建的,如下所示:randomHeuristicOne代码>
playHeuristicMove(randomHeuristicOne.RandomHeuristic())
也许你想看看你的游戏
游戏界面
对象它似乎从未被实例化过你说它从未被启动是什么意思?我正在创建它,然后我是返回的标签是通过它完成的吗?你是说别的吗?@SkatZ GameInterface game。。该字段似乎为空,因为您尚未创建实例。新操作员将为您创建一个实例,因此请尝试:GameInterface game=new GameInterface();(如果它是Java接口,那么使用implementer类)在制作游戏之前。labels.getFirst()callI尝试过,它仍然不起作用!还有其他想法为什么是空的吗?