Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4051266
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:10:24+00:00 2026-05-20T14:10:24+00:00

been looking in a dead gaze at my code and haven’t got anywhere. the

  • 0

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);
    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;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:10:25+00:00Added an answer on May 20, 2026 at 2:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive been looking for a proper rounding mechanism but nothing I find seems to
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I'm looking into experimenting with Tao but I am somewhat put-off by its dead-looking
Been looking for a solution for a long while now, but still no success,
Been looking around but couldn't find anything that explain how to do something like
Just been looking at a code golf question about generating a sorted list of
Ive been looking for this answer for hours, but I cant find it. I
Been looking ( maybe in the wrong place) , but I'm wondering how do
Been looking for a multi-threading solution for my application, but keep getting random NullReferenceException
I'm semi-new to android. I've been looking all over the internet, but I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.