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

时间:2011-03-07 21:50:17

标签: 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;
}

}

3 个答案:

答案 0 :(得分:0)

突然出现的第一件事是:

e.getActionCommand() == "buttonMenu"

应该是

e.getActionCommand().equals("buttonMenu");

如您希望比较String的值,而不是String对象是否相等。

答案 1 :(得分:0)

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

然后为您的NPE:添加一个新的Play实例作为所有按钮的动作侦听器。这些Play实例中的每一个都没有menuFhSFgameF,因为这些变量是由run方法初始化的。

MenuFrame和HighScoresFrame构造函数应该有一个Play参数,而run Play方法应该传递this作为播放参数,这样一个唯一的Play实例用来。这个独特的游戏应该被添加为动作倾听者。

答案 2 :(得分:0)

当您致电Play()时,您正在创建新的.addActionListener个对象。您希望将当前Play对象作为this参数传递,并使用该参数:

menuF = new MenuFrame(this);
hSF = new HighScoresFrame(this);
gameF = new GameFrame(this);

MenuFrame内部(例如):

private Play play;
public MenuFrame(Play play) {
    this.play = play;
}

menuButtonsDisplay内部:

JPanel menuButtonsDisplay() {
    ...
    highScores.addActionListener(this.play);
}