Java 空指针异常错误。。。有时

Java 空指针异常错误。。。有时,java,nullpointerexception,Java,Nullpointerexception,我正在写一段代码,玩家可以用从文本文件中随机选择的单词来扮演刽子手。我一直收到这个空指针异常错误,我不知道为什么。有时当我运行程序时,我没有问题。但有时当我运行时,由于出现空指针异常错误,程序立即崩溃。有人能帮我找出原因吗?这是我的密码: --编辑-- 尝试设置变量拼图时,displayGallows方法出错 import java.util.Scanner; import java.util.Random; import java.io.*; public class Hangman {

我正在写一段代码,玩家可以用从文本文件中随机选择的单词来扮演刽子手。我一直收到这个空指针异常错误,我不知道为什么。有时当我运行程序时,我没有问题。但有时当我运行时,由于出现空指针异常错误,程序立即崩溃。有人能帮我找出原因吗?这是我的密码:

--编辑-- 尝试设置变量拼图时,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;
        }
    }   
}
import java.util.Scanner;
导入java.util.Random;
导入java.io.*;
公共级刽子手{
专用扫描仪输入=新扫描仪(System.in);
私有板[]板={新板件(0)、新板件(0)、新板件(3),
新板件(1)、新板件(2)、新板件(0)};
私有字符串拼图;
私有字符串;
私有索引;
专用随机生成器=新随机(System.currentTimeMillis());
私人布尔赢;
私有布尔覆盖;
私有字符串correct=“”;
//私有字符串猜测=”;
私有字符猜测[]=新字符[26];
私人智力猜测;
私有整数未命中;
私有字符串拼图[]=新字符串[5];
//={“你好,世界”,“爪哇很酷”,“雅典娜很臭”,“扎克很棒”,“雅典娜很酷”};
公共静态void main(字符串[]args){
字符串字母;
再次使用String=“y”;
刽子手游戏=新刽子手();
试一试{
BufferedReader in=
新的BufferedReader(新的文件阅读器(“puzzles.txt”);
整数计数=0;
while(in.ready()){//当输入文件中还有另一行时
game.puzzles[count]=in.readLine();//获取数据行
count++;//递增CWID计数器
}
in.close();
}
捕获(IOE异常){
System.out.println(“读/写时出错”);
}

/*为了(int x=0;x您能检查哪一行出现空指针异常吗?大多数情况下,当对象未初始化以及您尝试执行操作时会发生空指针异常。因此,请确保在使用对象之前对其进行了初始化。

好的,这是编辑。调试了一段时间后,我发现当您阅读时,
谜题
数组未正确设置“puzzles.txt”文件。这段代码中出现了一些奇怪的情况。请确保正确读取了该文件,并且
puzzles
array获得了想要的内容

希望这对你来说是一个很好的起点

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
puzzword
上的值。这是了解实际情况的最好方法。

请检查文件puzzles.txt的路径。文件路径似乎不正确。在读/写过程中,您一定遇到了“打印异常”错误。请妥善处理,如果您无法读取文件,请不要继续


将puzzles.txt文件移动到正确的路径或提供准确的文件路径。

您的puzzles.txt是否有超过5行?因为您声明了一个
私有字符串puzzles[]=新字符串[5];
puzzword=puzzles[puzzleIndex];
试图在puzzleWord中输入空值时,问题似乎会发生

说你的谜题有:

aaaa
bbbb
cccc
当您声明您的拼图[]=新字符串[5]

你的拼图记忆是这样的:

null
null
null
null
null
之后,填充拼图数组:

aaaa
bbbb
cccc
null
null

当随机索引为3或4时,NPE发生。

NPE发生在哪一行?发布完整的堆栈跟踪更新问题。令人惊讶的是,它有时会起作用:您从未在仅包含空元素的拼图数组中放入任何内容。.当程序崩溃?似乎
puzzles[puzzleIndex]
导致
null
。它发生在displayGallows方法的第114行。我相信我正确初始化了对象,我无法找到错误所在。我想puzzleIndex=generator.nextInt(puzzles.length);在这里,在某些情况下,它无法找到nextInt,因此puzzleWord为null,导致它出现null指针异常。您能调试这一行吗?我实际上不知道如何调试,但将搜索如何调试。我们还没有学会如何调试。但将查找它!显然,我有4个单词,而不是5个。谢谢!