Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hangman GUI接收到空指针异常,无法找到位置_Java_Swing_User Interface_Nullpointerexception - Fatal编程技术网

Java Hangman GUI接收到空指针异常,无法找到位置

Java Hangman GUI接收到空指针异常,无法找到位置,java,swing,user-interface,nullpointerexception,Java,Swing,User Interface,Nullpointerexception,我目前正在做一个刽子手图形用户界面的硬件任务,我已经完成了我认为所有的代码,但是,我得到了一个nullpointerexception,我无法确定它的起源,这让我相当愤怒。我需要第二双眼睛来帮助我找出我在哪里犯了错误,以及我需要做些什么来纠正它。提前谢谢 NPE发生在: alphabet = "abcdefghijklmnopqrxtuvwxyz"; numLetters = 26; for (int count = 0; count < n

我目前正在做一个刽子手图形用户界面的硬件任务,我已经完成了我认为所有的代码,但是,我得到了一个nullpointerexception,我无法确定它的起源,这让我相当愤怒。我需要第二双眼睛来帮助我找出我在哪里犯了错误,以及我需要做些什么来纠正它。提前谢谢

NPE发生在:

alphabet = "abcdefghijklmnopqrxtuvwxyz";
            numLetters = 26;
            for (int count = 0; count < numLetters; count++) {
                letterChoice[count] = new JButton(Character.toString(alphabet
                        .charAt(count)));
                letterChoice[count].addActionListener(new CharacterListener(
                        alphabet.charAt(count)));
                letterChoice[count].setMnemonic(65 + count);
                add(letterChoice[count]);
            }
这是我所有的代码

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;

public class HangmanPanel extends JPanel {
    private JLabel imageLabel, numberLetters, gameOver, youWin;
    private JLabel[] spaces;
    private ImageIcon[] images;
    private JButton exitProgram, newGame, nextImage;
    private JButton[] letterChoice;
    private int imageNumber, letterNumber, numLetters, guesses;
    private WordList wordRand;
    private String word, alphabet;

    public HangmanPanel() {

        newGame = new JButton("New Game");
        newGame.setEnabled(true);
        newGame.setToolTipText("Press to restart game.");
        newGame.addActionListener(new NewGame());

        exitProgram = new JButton("Exit");
        exitProgram.setEnabled(true);
        exitProgram.setToolTipText("Press to close the program.");
        exitProgram.addActionListener(new ExitGame());

        wordRand = new WordList();
        word = wordRand.getWord();

        images = new ImageIcon[8];
        // Populating the array
        {
            images[0] = new ImageIcon("hangman0.png");
            images[1] = new ImageIcon("hangman1.png");
            images[2] = new ImageIcon("hangman2.png");
            images[3] = new ImageIcon("hangman3.png");
            images[4] = new ImageIcon("hangman4.png");
            images[5] = new ImageIcon("hangman5.png");
            images[6] = new ImageIcon("hangman6.png");
            images[7] = new ImageIcon("hangman7.png");
        }

        setBackground(Color.white);
        imageLabel = new JLabel(images[imageNumber]);
        imageNumber++;
        add(imageLabel);

        alphabet = "abcdefghijklmnopqrxtuvwxyz";
        numLetters = 26;
        for (int count = 0; count < numLetters; count++) {
            letterChoice[count] = new JButton(Character.toString(alphabet
                    .charAt(count)));
            letterChoice[count].addActionListener(new CharacterListener(
                    alphabet.charAt(count)));
            letterChoice[count].setMnemonic(65 + count);
            add(letterChoice[count]);
        }

        spaces = new JLabel[word.length()];
        while (letterNumber < spaces.length) {
            numberLetters = new JLabel("___");
            add(numberLetters);
            letterNumber++;
        }

        add(nextImage);
        add(newGame);
        add(exitProgram);

    }

    private class NewGame implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            imageLabel.setIcon(images[0]);
            imageNumber = 0;
            imageNumber++;
            imageLabel.repaint();
        }
    }

    private class ExitGame implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }

    private class CharacterListener implements ActionListener {
        public CharacterListener(char charAt) {

        }

        public void actionPerformed(ActionEvent e) {
            while (guesses < images.length) {
                int count = 0;
                while (count < word.charAt(count)) {
                    if (letterChoice[count].equals(word.charAt(count))) {
                        spaces[count] = new JLabel("" + letterChoice[count]
                                + "");
                        count++;
                    } else
                        imageLabel.setIcon(images[imageNumber]);
                    imageNumber++;
                    imageLabel.repaint();
                    guesses++;

                }

                if (guesses == 7) {
                    gameOver = new JLabel(
                            "You lose! Press New Game to try again!");
                    add(gameOver);
                } else
                    youWin = new JLabel(
                            "You win! Press New Game to play again or press Exit to remain Victorious!");
                add(youWin);
            }

        }

    }

}

NullPointerException应该告诉您是哪一行导致了问题。请您发布异常,我们将尽力帮助您找出其发生在特定行的原因

如果没有这些信息,可能会出现一些错误

第一个可能的错误。。。 您没有创建letterChoice数组-即在字母表之前的某个地方缺少以下内容

letterChoice = new JButton[26];
第二个可能的错误。。。 您没有创建下一个页面(即缺少以下内容):

newImage = new JButton("Blah");
第三个可能的错误

第四个可能的错误。。。
您的信件列表中没有“s”,而是有2个“s”字符。

请自己做一些腿部工作。NPE源于哪里?调试时您学到了什么?我相信它起源于这里alphabet=abcdefghijklmnopqrxtuvwxyz;numleters=26;对于整数计数=0;计数 public void actionPerformed(ActionEvent e) { while (guesses < images.length) { int count = 0; while (count < word.charAt(count)) { // probably here if (letterChoice[count].equals(word.charAt(count))) { // maybe here too spaces[count] = new JLabel("" + letterChoice[count] + ""); count++; } else imageLabel.setIcon(images[imageNumber]); imageNumber++; imageLabel.repaint(); guesses++; }
while (count < word.length && count < word.charAt(count)) {