线程中的异常" AWT-EventQueue-0" java.lang.NullPointerException Jswing问题

时间:2016-04-01 17:02:26

标签: java nullpointerexception jframe

您好,我试图在Jswing的方法中实际使用事件处理程序,当事件被触发时,我在线程中得到错误异常" AWT-EventQueue-0&# 34; java.lang.NullPointerException。我已经包含了GUI类,因此您可以了解发生了什么。问题在于方法setPlayerNameScreen及其中的事件处理程序。使用eclipse。

通常我的googlefu非常强大,因为这是我第一次在任何网站上发帖寻求帮助!任何帮助都是非常受欢迎的,因为我无法继续,直到我解决这个哈哈。

编辑:应该已经实际解释了代码的作用。当从前一个事件调用事件setPlayerNameScreen时,我们检查我们当前的玩家数量是否小于最大玩家数量,如果是,则将玩家名称更改为文本字段中的任何内容,将第1组添加到当前玩家计数并重置文本字段。

//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:完整错误

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)

1 个答案:

答案 0 :(得分:2)

有问题的陈述如下GameGUI

GameManager gameMngr = new GameManager();

在这里,您正在创建一个仅从该构造函数可见的局部变量。它与字段 gameMngr不同,我认为您应该写这些字段。通过将分配更改为简单来执行此操作:

gameMngr = new GameManager();

当然,setPlayerNameScreen()中的动作侦听器无法从构造函数访问局部变量,因此Java假设您指的是从未赋值的字段(null - 因此{{ 1}})。