![](/img/trans.png)
[英]Why am I getting a NullPointerException when I try to run my JFrame?
[英]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.