我正在编写一个代码,玩家可以使用从文本文件中选择的随机单词来玩hangman。我一直得到这个空指针异常错误,我不知道为什么。有时当我运行程序时,我没有问题。但有时当我运行时,程序立即崩溃,给我空指针异常错误。任何人都可以帮我找出原因吗?这是我的代码:
- Edit-- 在尝试设置变量拼图时,displayGallows方法出错。
import java.util.Scanner;
import java.util.Random;
import java.io.*;
public class Hangman {
private Scanner in = new Scanner(System.in);
private boardPiece[] board = {new boardPiece(0),new boardPiece(0),new boardPiece(3),
new boardPiece(1),new boardPiece(2),new boardPiece(0)};
private String puzzle;
private String puzzleWord;
private int puzzleIndex;
private Random generator = new Random(System.currentTimeMillis());
private boolean win;
private boolean over;
private String correct = "";
//private String guesses = "";
private char guesses[] = new char[26];
private int guessed;
private int misses;
private String puzzles[] = new String[5];
// = {"hello world","java is cool","athena is smelly","zach is awesome","athena is cool"};
public static void main(String [] args){
String letter;
String again = "y";
Hangman game = new Hangman();
try{
BufferedReader in =
new BufferedReader(new FileReader("puzzles.txt"));
int count = 0;
while (in.ready()) { //while there is another line in the input file
game.puzzles[count] = in.readLine(); //get line of data
count++; //Increment CWID counter
}
in.close();
}
catch(IOException e){
System.out.println("Error during reading/writing");
}
/*for(int x=0;x<game.puzzles.length;x++)
System.out.println("PUZZLE " + x + ": " + game.puzzles[x]);*/
System.out.println("Welcome to HangMan!");
System.out.println("To play, guess a letter to try to guess the word.");
System.out.println("Every time you choose an incorrect letter another");
System.out.println("body part appears on the gallows. If you guess the");
System.out.println("word before you're hung, you win");
System.out.println("If you get hung, you lose");
System.out.println("Time to guess...");
while(again.charAt(0) == 'y'){
game.displayGallows();
while(!game.over){
game.printBoard();
//System.out.println("Guessed: " + game.guesses);
game.printLettersGuessed();
System.out.println("The word so far: " + game.puzzle);
System.out.println("Choose a letter: ");
letter = game.in.next();
//game.guesses = game.guesses + " " + letter.charAt(0);
game.guesses[game.guessed] = letter.charAt(0);
game.guessed++;
game.sort();
if(game.puzzleWord.indexOf(letter.charAt(0)) != -1){
game.correct = game.correct + letter.charAt(0);
game.puzzle = game.puzzleWord.replaceAll("[^"+game.correct+" ]","-");
if(game.puzzleWord.replaceAll("["+game.correct+" ]","").length() == 0){
game.win = true;
game.over = true;
}
}
else
game.PrintWrongGuesses();
}
game.printBoard();
System.out.println("Solution: " + game.puzzleWord);
if(game.win)
System.out.println("Congratulations! You've solved the puzzle!");
else
System.out.println("You failed, failer!");
System.out.println();
System.out.println("Congratulations, you win!");
System.out.println("Do you want to play again? (y/n)");
again = game.in.next();
}
System.out.println("Goodbye!");
}
public void displayGallows(){
win = false;
over = false;
board[0].piece = " ______ ";
board[1].piece = " | | ";
board[2].piece = " | ";
board[3].piece = " | ";
board[4].piece = " | ";
board[5].piece = " _______| ";
puzzleIndex = generator.nextInt(puzzles.length);
puzzleWord = puzzles[puzzleIndex];
puzzle = puzzleWord.replaceAll("[A-Za-z]","-");
correct = "";
//guesses = "";
for(int x=0;x<26;x++)
guesses[x] = '~';
guessed = 0;
misses = 0;
}
public void printBoard(){
for(int x =0;x<6;x++)
System.out.println(board[x].piece);
}
public void PrintWrongGuesses(){
misses++;
System.out.println();
if(misses == 1){
board[2].piece = " 0 | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 2){
board[2].piece = " \\0 | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 3){
board[2].piece = " \\0/ | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 4){
board[3].piece = " | | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 5){
board[4].piece = " / | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 6){
board[4].piece = " / \\ | ";
System.out.println("Number of misses: " + misses);
over = true;
}
}
public void printLettersGuessed(){
System.out.print("Letters guessed already: ");
for(int x=0;x<26;x++){
if(guesses[x] != '~')
System.out.print(guesses[x] + " ");
}
System.out.println();
}
public void sort(){
boolean doMore = true;
while (doMore) {
doMore = false; // assume this is last pass over array
for (int i=0; i<guesses.length-1; i++) {
if (guesses[i] > guesses[i+1]) {
char temp = guesses[i];
guesses[i] = guesses[i+1];
guesses[i+1] = temp;
doMore = true; // after an exchange, must look again
}
}
}
}
class boardPiece{
public String piece;
public int total;
public int used;
boardPiece(int x){
used = 0;
total = x;
}
}
}
谢谢
答案 0 :(得分:1)
你的puzzles.txt有超过5行吗?因为你声明了一个private String puzzles[] = new String[5];
,当puzzleWord = puzzles[puzzleIndex];
试图将一个null放入puzzleWord时,似乎会出现问题。
说你的谜题有:
aaaa
bbbb
cccc
当你宣布你的谜题[] = new String [5];
你的谜题记忆如下:
null
null
null
null
null
之后填充拼图数组:
aaaa
bbbb
cccc
null
null
当randomed指数为3或4时,NPE发生。
答案 1 :(得分:0)
可以检查哪一行出现空指针异常。当对象未初始化并且您尝试进行操作时,通常会发生这种情况。因此,在使用之前确保对象已初始化。
答案 2 :(得分:0)
好的,这是编辑。调试一段时间后,我发现当你读“puzzles.txt”文件时没有正确设置puzzles
数组。这段代码中有一些时髦的东西。确保正确读取文件并使puzzles
数组获得预期的内容。
希望这对你来说是一个很好的起点。
try{
BufferedReader in =
new BufferedReader(new FileReader("puzzles.txt"));
int count = 0;
while (in.ready()) { //while there is another line in the input file
game.puzzles[count] = in.readLine(); //get line of data
count++; //Increment CWID counter
}
in.close();
}
catch(IOException e){
System.out.println("Error during reading/writing");
}
为什么不尝试对其进行调试,并在方法中查看puzzles
和puzzleWord
上的值。这是了解真实情况的最佳方式。
答案 3 :(得分:-1)
请检查文件puzzles.txt的路径。看来文件路径不正确。你必须得到一个例外打印“阅读/写作时出错”。正确处理,如果您无法读取文件,请不要继续操作。
将puzzles.txt文件移动到正确的路径或提供准确的文件路径。