我运行程序时为什么会出现NullPointerException?

时间:2014-12-20 19:55:47

标签: java nullpointerexception

我基本上是在制作一个刽子手游戏......

package wordguessinggame2;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class WordGuessingGame2 {
    private static String playerInput;
    private static String randomWord;

    static class RandomWordProvider {

        public final List<String> words;

        public RandomWordProvider() {
            words = readFile();
        }

        public int randomInteger() {
            int randomInt = (int) (Math.random() * words.size());
            return randomInt;
        }

        public String getWord() {
            int randomPosition = randomInteger();
            String randomWord = words.get(randomPosition);
            return randomWord;
        }

        private List<String> readFile() {

            List<String> wordsList = new ArrayList<>();

            try {
                File fourLetterWords = new File(System.getProperty("user.home"),"Documents/FourLetterWords.txt");
                Scanner in = new Scanner(fourLetterWords);

                while (in.hasNextLine()) {
                    String line = in.nextLine();
                    if (line!=null && !line.isEmpty()) {
                        wordsList.add(line);
                    }
                }
            } 
            catch (FileNotFoundException ex) {
                System.out.println("\nFile not found.");
                System.exit(0);
            }
            return wordsList;
        }
    }

    public WordGuessingGame2(String playerInput) {
        WordGuessingGame2.playerInput = playerInput;
    }
    public String getPlayerInput() {
        return playerInput;
    }
    public void setPlayerInput(String playerInput) {
        WordGuessingGame2.playerInput = playerInput;
    }

    public static class PlayerCharacterEntry{    
        private String playerEntry() {
        Scanner characterEntry = new Scanner(System.in);
        System.out.print("Enter a character: ");
        String playerInput = characterEntry.next();
        playerInput = playerInput.toUpperCase();
        return playerInput;
        }
    }


    public static void main(String[] args) {

        Scanner wantToPlay = new Scanner(System.in);
        System.out.print("Welcome to the word guessing game! Would you like to play? ");
        String playerAnswer = wantToPlay.next();

        if (playerAnswer.equalsIgnoreCase("Yes")) {
            System.out.print("\nYour objective is to guess a four letter word by entering"
            + "\nletters on your keyboard. If you can not guess the word in seven attempts,"
            + "\nyou lose! You will be told if the letter you entered is in the word, and"
            + "\nyou will be told if the letter you entered is not in the word. You will be"
            + "\nallowed to guess the word any time during your seven attempts. If at anytime"
            + "\nyou would like to terminate the game, enter the word 'terminate'. Good Luck!"
            + "\n \n");
        }
        if (playerAnswer.equalsIgnoreCase("No")) {
            System.out.print("Maybe another time!");
            System.exit(0);
        }

        RandomWordProvider randomWordProvider = new RandomWordProvider();
        PlayerCharacterEntry playerCharacterEntry = new PlayerCharacterEntry();

        randomWordProvider.getWord();
        playerCharacterEntry.playerEntry();


        if (randomWord.contains(playerInput)){
            System.out.println(playerInput);
        } else {
            System.out.println("That letter is not in the word!");
        }
    }
}

在底部,我试图确定播放器输入的字母是否在单词中,然后显示该字母,但是当我运行程序并输入一个字母时,我得到一个NullPointerException,其中我说:

if(randomWord.contains(playerInput))

我认为它与randomWord有关,但我不知道如何解决它。

3 个答案:

答案 0 :(得分:2)

您永远不会向WordGuessingGame2.randomWord分配任何内容null

您需要替换

randomWordProvider.getWord();

randomWord = randomWordProvider.getWord();

答案 1 :(得分:0)

你必须设置正确的路径..试试这个:

  

File fourLetterWords = new File(System.getProperty(“user.home”),“/ Documents / FourLetterWords.txt”);

答案 2 :(得分:0)

你永远不会初始化类变量randomWord