0

Hi so I'm trying to essentially use an event handler within a method for Jswing and when the event is triggered I get the error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException. I have included the GUI class so you can get an idea what's happening. The issue is with the method setPlayerNameScreen and the event handler within it. Using eclipse.

Usually my googlefu is pretty strong as this is my first time posting on any site for help! Any help is massively appreciated as I can't move on till i fix this haha.

Edit: Should have actually explained what the code does. When the event setPlayerNameScreen is called from a previous event, we check if our current player count is less than max players and if it is, change the players name to whatever is in the text field, add set 1 to our current player count and reset the text field.

//Main game GUI class
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.math.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GameGUI extends JFrame 
{

private JPanel startPanel;
private JPanel playerNamePanel;
private JTextField txtPlayerNumber;
GameManager gameMngr;
private int currentPlayer;

/**
 * Launch the application.
 */
public static void main(String[] args) 
{
    EventQueue.invokeLater(new Runnable() 
    {
        public void run() 
        {
            try 
            {
                GameGUI frame = new GameGUI();
                frame.setVisible(true);
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public GameGUI() 
{


    //Setup our game manager object
    GameManager gameMngr = new GameManager();
    //int for tracking current active player
    int currentPlayer = 1;

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    startPanel = new JPanel();
    startPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(startPanel);
    startPanel.setLayout(null);

    JLabel lblTitle = new JLabel("Who Wants To Be a Zillionaire!");
    lblTitle.setBounds(5, 5, 424, 14);
    lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
    startPanel.add(lblTitle);

    JLabel lblPlayerNumber = new JLabel("Please Enter How Many Players (max 4)");
    lblPlayerNumber.setBounds(99, 55, 243, 92);
    lblPlayerNumber.setHorizontalAlignment(SwingConstants.CENTER);
    startPanel.add(lblPlayerNumber);

    txtPlayerNumber = new JTextField();
    txtPlayerNumber.setHorizontalAlignment(SwingConstants.CENTER);
    txtPlayerNumber.setBounds(116, 153, 200, 20);
    startPanel.add(txtPlayerNumber);
    txtPlayerNumber.setColumns(10);

    //action listener for game start screen button
    JButton btnStart = new JButton("Start!");
    btnStart.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent arg0) 
        {
            //button has been pressed so pass player count and prepare for player names
            int playerCount = Integer.valueOf(txtPlayerNumber.getText());
            gameMngr.setPlayerCount(playerCount);
            //set current panel invisible
            startPanel.setEnabled(false);
            startPanel.setVisible(false);
            setPlayerNameScreen();

        }
    });
    btnStart.setBounds(171, 200, 89, 23);
    startPanel.add(btnStart);
}

private void setPlayerNameScreen()
{

    //setup the player name setting panel
    playerNamePanel = new JPanel();
    playerNamePanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(playerNamePanel);
    playerNamePanel.setLayout(null);

    JLabel lblPlayerName = new JLabel("Player 1 Enter Your Name");
    lblPlayerName.setHorizontalAlignment(SwingConstants.CENTER);
    lblPlayerName.setBounds(87, 11, 259, 14);
    playerNamePanel.add(lblPlayerName);

    JTextField txtPlayerName = new JTextField();
    txtPlayerName.setHorizontalAlignment(SwingConstants.CENTER);
    txtPlayerName.setBounds(172, 81, 86, 20);
    playerNamePanel.add(txtPlayerName);
    txtPlayerName.setColumns(10);



    JButton btnPlayerNameConf = new JButton("OK");

    btnPlayerNameConf.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e) 
        {

            if(currentPlayer <= gameMngr.getPlayerCount())
            {
                gameMngr.addPlayer(txtPlayerName.getText(), currentPlayer);
                currentPlayer++;
                txtPlayerName.setText("");
                lblPlayerName.setText("Player " + currentPlayer + " Enter Your Name");
            }
        }
    });
    btnPlayerNameConf.setBounds(172, 142, 89, 23);
    playerNamePanel.add(btnPlayerNameConf);
}

}

Edit2: Full error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at GameGUI$3.actionPerformed(GameGUI.java:132)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Ravi Ranjan
  • 740
  • 2
  • 10
  • 31
Feetz
  • 13
  • 3

1 Answers1

2

The problematic statement is as follows in GameGUI:

GameManager gameMngr = new GameManager();

Here, you are creating a local variable visible from just that constructor. It is different to the field gameMngr, which I think you should be writing to. Do so by changing the assignment to simply:

gameMngr = new GameManager();

Of course, the action listener in setPlayerNameScreen() cannot access the local variable from the constructor, so Java assumes you mean the field, which is never assigned to (it is null - hence the NullPointerException).

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
  • You sir. Deserve more than 1 cookie. Such a silly mistake and I didn't consider looking there for it. The code executes as expected now. Thankyou so much! – Feetz Apr 01 '16 at 17:38