java - 运行actionPerformed时出现空指针异常,Java

标签 java exception pointers null jframe

一直死死地盯着我的代码,但一无所获。程序编译得很好,但每当 Play.java 运行“actionPerformed”时,我都会从“AWT-EventQueue-0”线程收到空指针异常,或者用简单的英语来说,当我单击按钮时,它会抛出 NPE。这是代码(如果您想在自己的计算机上运行它,您可能需要在编译时省略一些行)。

提前非常感谢任何帮助

Play.java :-

public class Play implements Runnable, ActionListener { 
private MenuFrame menuF;    
private HighScoresFrame hSF;
private GameFrame gameF;

public static void main(String[] args) {
    Play program = new Play();
    SwingUtilities.invokeLater(program);
}

public void run() {
    menuF = new MenuFrame();
    hSF = new HighScoresFrame();
    gameF = new GameFrame();
}

void playGame() {
    System.out.print("running the game!");
}

public void actionPerformed (ActionEvent e) {
    if (e.getActionCommand() == "buttonMenu") {
        menuF.setVisible(true);
        hSF.setVisible(false);
    }
    if (e.getActionCommand() == "buttonPlayGame") {          
        gameF.setVisible(true);
        menuF.setVisible(false);
        playGame();
    }
    if (e.getActionCommand() == "buttonHighScores") {
        hSF.setVisible(true);
        menuF.setVisible(false);
    }
}

}

GameFrame.java :-

public class GameFrame extends JFrame {

private static final int WINDOW_WIDTH = 1024;
private static final int WINDOW_HEIGHT = 576;
private JFrame game;

public GameFrame() {
    game = new JFrame();

    game.setPreferredSize(new Dimension(WINDOW_WIDTH, (WINDOW_HEIGHT + 20)));

    game.setResizable(false);
    game.setDefaultCloseOperation(game.EXIT_ON_CLOSE);
    game.setTitle("COMS11300 Crisis - in game");
    game.pack();
    game.setLocationRelativeTo(null);
    game.setVisible(true);
}

}

HighScoresFrame.java :-

public class HighScoresFrame extends JFrame {

private static final int WINDOW_WIDTH = 1024;
private static final int WINDOW_HEIGHT = 576;
private int highscores[] = new int[5];
private JFrame hS;

public HighScoresFrame() {
    hS = new JFrame();

    hS.setPreferredSize(new Dimension(WINDOW_WIDTH, (WINDOW_HEIGHT + 20)));
    hS.getContentPane().add(highscoresLayeredPane());
    hS.setResizable(false);
    hS.setDefaultCloseOperation(hS.EXIT_ON_CLOSE);
    hS.setTitle("COMS11300 Crisis - High Scores");
    hS.pack();
    hS.setLocationRelativeTo(null);
    hS.setVisible(false);
}

JLayeredPane highscoresLayeredPane() {
    JLayeredPane lPane = new JLayeredPane();

    lPane.setBounds(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
    lPane.add(highscoresBackgroundDisplay(), new Integer(0), 0);
    lPane.add(highscoresButtonsDisplay(), new Integer(1), 0);
    lPane.add(highscoresDisplay(), new Integer(2), 0);

    return lPane;
}

ImagePanel highscoresBackgroundDisplay() {
    URL u = this.getClass().getResource("images/background_highscores.png");
    ImageIcon imgIcon = new ImageIcon(u);
    ImagePanel backgroundPanel = new ImagePanel(imgIcon.getImage());

    backgroundPanel.setBounds(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
    backgroundPanel.setOpaque(true);

    return backgroundPanel;
}

JPanel highscoresButtonsDisplay() {
    JPanel buttonPanel = new JPanel();
    JButton menu = new JButton();
    URL uMenu = this.getClass().getResource("images/button_play.png");
    ImageIcon imgMenuIcon = new ImageIcon(uMenu);

    menu.setIcon(imgMenuIcon);
    menu.setBorder(null);
    menu.setActionCommand("buttonMenu");
    menu.addActionListener(new Play());

    buttonPanel.setBounds(730, 355, 280, 200);
    buttonPanel.setOpaque(false);
    buttonPanel.add(menu);

    return buttonPanel;
}

String retrieveHighScores() {
    String highscoresString = new String();

    for (int i = 0; i < 5; i++) {
        highscoresString += highscores[i];
        highscoresString += "\n";
    }

    return highscoresString;
}

JPanel highscoresDisplay() {
    JPanel scoresPanel = new JPanel();
    JLabel scoresLabel = new JLabel(retrieveHighScores(), JLabel.LEFT);

    scoresPanel.setBounds(100, 100, 200, 200);
    scoresPanel.setOpaque(false);
    scoresPanel.add(scoresLabel);

    return scoresPanel;
}

}

MenuFrame.java :-

public class MenuFrame extends JFrame {

private static final int WINDOW_WIDTH = 1024;
private static final int WINDOW_HEIGHT = 576;
private JFrame menu;

public MenuFrame() {
    menu = new JFrame();

    menu.setPreferredSize(new Dimension(WINDOW_WIDTH, (WINDOW_HEIGHT + 20)));
    menu.getContentPane().add(menuLayeredPane());
    menu.setResizable(false);
    menu.setDefaultCloseOperation(menu.EXIT_ON_CLOSE);
    menu.setTitle("COMS11300 Crisis - Main Menu");
    menu.pack();
    menu.setLocationRelativeTo(null);
    menu.setVisible(true);
}

JLayeredPane menuLayeredPane() {
    JLayeredPane lPane = new JLayeredPane();

    lPane.setBounds(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
    lPane.add(menuBackgroundDisplay(), new Integer(0), 0);
    lPane.add(menuButtonsDisplay(), new Integer(1), 0);

    return lPane;
}

ImagePanel menuBackgroundDisplay() {
    URL u = this.getClass().getResource("images/background_menu.png");
    ImageIcon imgIcon = new ImageIcon(u);
    ImagePanel backgroundPanel = new ImagePanel(imgIcon.getImage());

    backgroundPanel.setBounds(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
    backgroundPanel.setOpaque(true);

    return backgroundPanel;
}

JPanel menuButtonsDisplay() {
    JPanel buttonPanel = new JPanel();
    JButton play = new JButton();
    JButton highScores = new JButton();
    URL uPlay = this.getClass().getResource("images/button_play.png");
    URL uHighScore = this.getClass().getResource("images/button_highscore.png");
    ImageIcon imgPlayIcon = new ImageIcon(uPlay);
    ImageIcon imgHighScoreIcon = new ImageIcon(uHighScore);

    play.setIcon(imgPlayIcon);
    play.setBorder(null);
    play.setActionCommand("buttonPlayGame");
    play.addActionListener(new Play());

    highScores.setIcon(imgHighScoreIcon);
    highScores.setBorder(null);
    highScores.setActionCommand("buttonHighScores");
    highScores.addActionListener(new Play());

    buttonPanel.setBounds(730, 355, 280, 200);
    buttonPanel.setOpaque(false);
    buttonPanel.add(play);
    buttonPanel.add(highScores);

    return buttonPanel;
}

}

最佳答案

首先,你永远不应该将字符串与 == 进行比较。使用 equals 来比较字符串的内容,而不是引用。

然后对于您的 NPE:您添加一个新的 Play 实例作为所有按钮的操作监听器。这些 Play 实例中的每一个都没有任何 menuF , hSFgameF ,因为这些变量是由 run 初始化的方法。

MenuFrame 和 HighScoresFrame 构造函数应该有 Play参数,以及 run方法Play应该通过this作为 play 参数,以便使用唯一的 Play 实例。这种独特的玩法应该添加为 Action 监听器。

关于java - 运行actionPerformed时出现空指针异常,Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5225698/

相关文章:

java - 在 Internet Explorer 11 中运行 Java

python - __getitem__/__setitem__ 应该与不受支持的切片步长一起使用哪种异常?

python - 函数定义周围的 try- except 会忽略异常处理

c++ - 这是指针的问题吗?

Java6、Guava、泛型、类型推断

java - 使用 Jackson 将 MongoDB 日期字段反序列化为 Java POJO

java - 如何连接后端的Spring WebSocket请求?

c# - 具有函数作用域的变量

c++ - 从 QMap 中删除所有值

c++ - 智能指针的模板特化与普通指针完全一样