簡體   English   中英

為什么在運行程序時出現NullPointerException?

[英]Why am I getting a NullPointerException when I run my program?

我基本上是在做一個子手游戲。

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有關,但我不知道如何解決。

您永遠不會將任何內容分配給WordGuessingGame2.randomWord ,使其保持為null

您需要更換

randomWordProvider.getWord();

randomWord = randomWordProvider.getWord();

您必須輸入正確的路徑..試試這個:

文件fourLetterWords =新文件(System.getProperty(“ user.home”),“ / Documents / FourLetterWords.txt”);

您永遠不會初始化類變量randomWord

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM