如何制作getArray方法

时间:2013-01-18 20:32:10

标签: arrays

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

 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的运行时错误。我四处询问,他们说我应该制作一个方法来获取数组字,因为我现在无法达到它。这种方法应该包含什么?

1 个答案:

答案 0 :(得分:0)

我相信你的问题在这里:

private String myGeneratedRandomWord = getWords();

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

这应解决您的NullPointerError。