0

当我运行代码的特定部分时,我有:

 public String getWords (){
    randomWord = words[randy.nextInt(words.length)];
    return randomWord;
}

private String[] words = {"apple", "beret", "arose", "along", "beamy", "becks", "decks", "barks",
                              "stark", "start", "stabs", "baggy", "asked", "asset", "asses", "audit",
                              "bowls", "boxes", "seats", "balls", "boats", "boxer", "brick", "bound",
                              "brass", "caked", "braid", "caged", "essay", "fault", "dents", "dutch",
                              "ethos", "dunks", "pains", "faxes", "mummy", "mixer", "mills", "might",
                              "moral", "teeth", "wings", "works", "walls", "tolls", "crawl", "toxin",
                              "bangs", "tough"};


Hangman man = new Hangman();
      man.mainScreen();



public void mainScreen (){
        start();
        while (guesses != maxGuesses){
            continueGame();
            checkBodyParts();
        }
        gameOver();
    }

错误:

java.lang.NullPointerException
at Hangman.getWords(Hangman.java:43)
at Hangman.<init>(Hangman.java:28)
at GameApp.main(GameApp.java:9)

我收到 NullPointerException 的运行时错误。我四处询问,他们说我应该制定一种方法来获取数组单词,因为我目前无法获得它。这个方法应该有什么?

4

1 回答 1

0

我相信你的问题在这里:

private String myGeneratedRandomWord = getWords();

此方法试图在创建数组“words”之前使用它。在创建数组“words”后移动这一行。

这应该可以解决您的 NullPointerError。

于 2013-01-19T01:29:06.913 回答