简体   繁体   中英

Why am I getting a NullPointerException when I run my program?

I am basically making a hangman game...

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!");
        }
    }
}

Here at the bottom I am trying to determine if the letter that the player enters is in the word, then to display that letter, but when I run the program and I input a letter, I get a NullPointerException where I say:

if(randomWord.contains(playerInput))

I believe it has something to do with randomWord but I don't know how to fix it.

You never assign anything to WordGuessingGame2.randomWord to it stays null .

You need to replace

randomWordProvider.getWord();

with

randomWord = randomWordProvider.getWord();

you have to put a correct path.. try this :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM