返回JLabel时出现NullPointerException

时间:2013-06-15 11:11:53

标签: random nullpointerexception shuffle

我似乎有一个nullPointerException没有任何理由,请你查看我的代码并告诉我你的意见? 这是我在另一个类上调用的类和构造函数,以便从链表中随机获取标签(使用随机化的shuffle)。

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我在调试时检查它是否正常工作,但是当我调用它时仍然会得到一个nullPointer异常。 randomHeuristicOne是在同一个类上创建的,如下所示:RandomHeuristic randomHeuristicOne; playHeuristicMove(randomHeuristicOne.RandomHeuristic());

1 个答案:

答案 0 :(得分:2)

也许您想看看您的game GameInterface对象,它似乎从未被实例化