开发者

Null pointer exception when running actionPerformed, Java

been looking in a dead gaze at my code and haven't got anywhere. the program compiles fine, but whenever Play.java runs 'actionPerformed' i get a Null Pointer Exception from the 'AWT-EventQueue-0' thread, or in plain english, when i click the button it throws a NPE. Here's the code (if you want to run it on your own computer you might need to omit some lines on compilation).

Any help is much appreciated in advance

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);
    button开发者_JAVA技巧Panel.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;
}

}


First of all, you should never compare Strings with ==. Use equals to compare the contents of the strings, rather than the reference.

Then for your NPE : you add a new Play instance as action listener for all your buttons. Each of these Play instance doesn't have any menuF, hSF or gameF, since these variables are initialized by the run method.

The MenuFrame and HighScoresFrame constructors should have a Play argument, and the run method of Play should pass this as the play argument, so that a unique Play instance is used. This unique play is the one that should be added as action listener.


The first thing that pops out to me is:

e.getActionCommand() == "buttonMenu"

should be

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

As you wish to compare the value of the String, not whether the String objects are equal.


You are creating new Play() objects when you call .addActionListener. You want to pass the current Play object as this parameter, and use that instead:

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

Inside of MenuFrame (for example):

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

Inside of menuButtonsDisplay:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜