【问题标题】:NullPointerException with JButton Action Listener带有 JButton 动作侦听器的 NullPointerException
【发布时间】:2020-07-27 15:34:49
【问题描述】:

我是编码新手,我正在尝试制作基于文本、基于决策(因此有 1A 变量和 1B 变量)的游戏。当我单击 JButton west 以唤起动作侦听器时,程序取消并给我这个 NullPointerException 错误。

我尝试过使用 Eclipse 调试器,但是当我这样做时,会弹出一个窗口说“JDI 线程评估遇到了问题”。关于如何解决此问题的任何想法?

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class The_Room {
    
    JFrame window;
    Container container;
    
    JPanel titleNamePanel;
    JPanel mainPanel;
    JPanel textPanel1A;
    JPanel go_northButtonPanel;
    JPanel westButtonPanel;
    JPanel textPanel2A;
    JPanel lift_paintingButtonPanel;
    JPanel go_west_insteadButtonPanel;
    JPanel lift_mattressButtonPanel;
    JPanel go_north_insteadButtonPanel;
    
    JButton startButton;
    JButton go_north;
    JButton west;
    JButton lift_painting;
    JButton go_west_instead;
    JButton lift_mattress;
    JButton go_north_instead;
    
    JTextArea TextArea1A; //A JLabel that allows multiple lines of text
    JTextArea TextArea2A;
    JTextArea TextArea2B;
    
    JLabel titleNameLabel;
    
    TitleScreenHandler handler = new TitleScreenHandler();
    Screen1AHandler handler1A = new Screen1AHandler();
    Screen1BHandler handler1B = new Screen1BHandler();
    
    public static void main(String[] args) {
        new The_Room();
    }
    
    public The_Room() {
        window = new JFrame();
        window.setSize(800, 600);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().setBackground(Color.MAGENTA); //you can't change color w/o .getContentPane() 
        window.setLayout(null); //set it to null now (the default), so that you can customize the layout later
        window.setVisible(true);
        window.setTitle("THE ROOM");
        container = window.getContentPane();
        
        titleNamePanel = new JPanel();
        titleNamePanel.setBounds(10, 100, 800, 150); 
        titleNamePanel.setBackground(Color.MAGENTA);
        titleNameLabel = new JLabel("THE ROOM");
        container.add(titleNamePanel); 
        titleNamePanel.add(titleNameLabel);
        titleNameLabel.setFont(new java.awt.Font("Arial", Font.BOLD, 120));
        titleNameLabel.setOpaque(true);
        titleNameLabel.setBackground(Color.MAGENTA);
        titleNameLabel.setForeground(Color.WHITE); 
        
        startButton = new JButton();
        startButton.setText("START");
        startButton.setFont(new java.awt.Font("Arial", Font.BOLD, 50));
        startButton.setBounds(300, 300, 200, 100);
        startButton.addActionListener(handler);
        container.add(startButton);
        
    }
    
    public void createGameScreen() {
            
            titleNamePanel.setVisible(false); 
            startButton.setVisible(false);
            
            
            go_northButtonPanel = new JPanel();
            go_northButtonPanel.setBounds(300,400,200,100);
            go_northButtonPanel.setBackground(Color.MAGENTA);
            
            go_north = new JButton();
            go_north.setBackground(Color.BLACK);
            go_north.setForeground(Color.BLACK);
            go_north.setFont(new java.awt.Font("Arial", Font.BOLD, 25));
            go_north.setBounds(300, 300, 200, 100);
            go_north.addActionListener(handler1A);
            go_northButtonPanel.add(go_north);
            go_north.setText("GO NORTH");
            container.add(go_northButtonPanel);
            
            westButtonPanel = new JPanel();
            westButtonPanel.setBounds(300,500,200,200);
            westButtonPanel.setBackground(Color.MAGENTA);
            container.add(westButtonPanel);
            
            west = new JButton();
            west.setBackground(Color.BLACK);
            west.setForeground(Color.BLACK);
            west.setFont(new java.awt.Font("Arial", Font.BOLD, 15));
            west.setBounds(300, 500, 200, 200);
            west.addActionListener(handler1B);
            westButtonPanel.add(west);
            west.setText("GO WEST");
            
            textPanel1A = new JPanel();
            textPanel1A.setBounds(0,100,800,850);
            textPanel1A.setBackground(Color.MAGENTA);
            container.add(textPanel1A);
            
            TextArea1A = new JTextArea("You wake up in a small, cell-like room. Where are you? Who are you? You don't remember anything, but all you know is that you're trapped inside this room and you need to ESCAPE.");
            TextArea1A.setBounds(0,100,800,850);
            TextArea1A.setBackground(Color.MAGENTA);
            TextArea1A.setForeground(Color.BLACK);
            TextArea1A.setFont(new java.awt.Font("Arial", Font.PLAIN, 20));
            TextArea1A.setLineWrap(true);
            TextArea1A.setWrapStyleWord(true);
            textPanel1A.add(TextArea1A);
            
            
    }
    
    public void createGameScreen2A() {
        go_northButtonPanel.setVisible(false);
        westButtonPanel.setVisible(false);
        west.setVisible(false);
        textPanel1A.setVisible(false);
        TextArea1A.setVisible(false);
        
        lift_paintingButtonPanel = new JPanel();
        lift_paintingButtonPanel.setBounds(300,400,200,100);
        lift_paintingButtonPanel.setBackground(Color.MAGENTA);
        
        lift_painting = new JButton();
        lift_painting.setBackground(Color.BLACK);
        lift_painting.setForeground(Color.BLACK);
        lift_painting.setFont(new java.awt.Font("Arial", Font.BOLD, 25));
        lift_painting.setBounds(300, 300, 200, 100);
        //lift_painting.addActionListener(handler1A);
        lift_paintingButtonPanel.add(lift_painting);
        lift_painting.setText("LIFT PAINTING");
        container.add(lift_paintingButtonPanel); 
        
        go_west_insteadButtonPanel = new JPanel();
        go_west_insteadButtonPanel.setBounds(300,500,200,200);
        go_west_insteadButtonPanel.setBackground(Color.MAGENTA);
        
        go_west_instead = new JButton();
        go_west_instead.setBackground(Color.BLACK);
        go_west_instead.setForeground(Color.BLACK);
        go_west_instead.setFont(new java.awt.Font("Arial", Font.BOLD, 25));
        go_west_instead.setBounds(300, 500, 200, 100);
        go_west_insteadButtonPanel.add(go_west_instead);
        go_west_instead.setText("GO WEST");
        container.add(go_west_insteadButtonPanel);
        
        textPanel2A = new JPanel();
        textPanel2A.setBounds(0,100,800,850);
        textPanel2A.setBackground(Color.MAGENTA);
        container.add(textPanel2A);
        
        TextArea2A = new JTextArea("You wake up in a small, cell-like room. Where are you? Who are you? You don't remember anything, but all you know is that you're trapped inside this room and you need to ESCAPE.");
        TextArea2A.setBounds(0,100,800,850);
        TextArea2A.setBackground(Color.MAGENTA);
        TextArea2A.setForeground(Color.BLACK);
        TextArea2A.setFont(new java.awt.Font("Arial", Font.PLAIN, 20));
        TextArea2A.setLineWrap(true);
        TextArea2A.setWrapStyleWord(true);
        textPanel2A.add(TextArea2A);
        
    }
    
    public void createGameScreen2B() {
        lift_paintingButtonPanel.setVisible(false); //LINE 190, where the code stops working
        go_west_insteadButtonPanel.setVisible(false);
        go_west_instead.setVisible(false);
        textPanel2A.setVisible(false);
        TextArea2A.setVisible(false);
        
        lift_mattressButtonPanel = new JPanel();
        lift_mattressButtonPanel.setBounds(300,400,200,100);
        lift_mattressButtonPanel.setBackground(Color.MAGENTA);
        
        lift_mattress = new JButton();
        lift_mattress.setBackground(Color.BLACK);
        lift_mattress.setForeground(Color.BLACK);
        lift_mattress.setFont(new java.awt.Font("Arial", Font.BOLD, 25));
        lift_mattress.setBounds(300, 300, 200, 100);
        //lift_mattress.addActionListener(handler2B);
        lift_mattressButtonPanel.add(lift_mattress);
        lift_mattress.setText("LIFT PAINTING");
        container.add(lift_mattressButtonPanel); 
    }
    
    public class TitleScreenHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            createGameScreen();
        }
        }
    
    public class Screen1AHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            createGameScreen2A();
        }
    }
    
    public class Screen1BHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            createGameScreen2B(); //LINE 225
        }
    }
}
Stack trace:

Thread [AWT-EventQueue-0] (Suspended)   
    The_Room.createGameScreen2B() line: 190 
    The_Room$Screen1BHandler.actionPerformed(ActionEvent) line: 225 
    JButton(AbstractButton).fireActionPerformed(ActionEvent) line: 1967 
    AbstractButton$Handler.actionPerformed(ActionEvent) line: 2308  
    DefaultButtonModel.fireActionPerformed(ActionEvent) line: 405   
    DefaultButtonModel.setPressed(boolean) line: 262    
    AquaButtonUI$AquaButtonListener(BasicButtonListener).mouseReleased(MouseEvent) line: 279    
    JButton(Component).processMouseEvent(MouseEvent) line: 6632 
    JButton(JComponent).processMouseEvent(MouseEvent) line: 3342    
    JButton(Component).processEvent(AWTEvent) line: 6397    
    JButton(Container).processEvent(AWTEvent) line: 2263    
    JButton(Component).dispatchEventImpl(AWTEvent) line: 5008   
    JButton(Container).dispatchEventImpl(AWTEvent) line: 2321   
    JButton(Component).dispatchEvent(AWTEvent) line: 4840   
    LightweightDispatcher.retargetMouseEvent(Component, int, MouseEvent) line: 4918 
    LightweightDispatcher.processMouseEvent(MouseEvent) line: 4547  
    LightweightDispatcher.dispatchEvent(AWTEvent) line: 4488    
    JFrame(Container).dispatchEventImpl(AWTEvent) line: 2307    
    JFrame(Window).dispatchEventImpl(AWTEvent) line: 2772   
    JFrame(Component).dispatchEvent(AWTEvent) line: 4840    
    EventQueue.dispatchEventImpl(AWTEvent, Object) line: 772    
    EventQueue$4.run() line: 721    
    EventQueue$4.run() line: 715    
    AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]    
    ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext, AccessControlContext) line: 85   
    ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext) line: 95 
    EventQueue$5.run() line: 745    
    EventQueue$5.run() line: 743    
    AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]    
    ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext, AccessControlContext) line: 85   
    EventQueue.dispatchEvent(AWTEvent) line: 742    
    EventDispatchThread.pumpOneEventForFilters(int) line: 203   
    EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: 124    
    EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: 113   
    EventDispatchThread.pumpEvents(int, Conditional) line: 109  
    EventDispatchThread.pumpEvents(Conditional) line: 101   
    EventDispatchThread.run() line: 90

【问题讨论】:

    标签: listener


    【解决方案1】:

    我看到你已经修复了你的代码,但我想让你知道你命名你的类和变量没有根据 java 约定。虽然这不会导致编译错误,但它确实会降低您的代码在其他 Java 开发人员中的可读性,如果您想成为一名专业 (Java) 开发人员,您绝对应该更改这些内容。您现在在某些地方正确地执行了此操作,而下一行则错误地执行了。

    所有变量名都应该是camelCased。所以没有下划线。例如goNorthButtonPanel。我们只在常量中使用下划线(static final)并且这些都应该是大写的(例如SOME_CONSTANT_VALUE) 类名应始终以大写字母开头,也应为驼峰式,因此不能使用下划线 (TheRoom)。 您所有的方法名称都是正确的,所以请继续努力:)

    有关所有 Java 约定的概述,您可以查看 https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html

    【讨论】:

      【解决方案2】:

      go_northButtonPanel 在您按下“向北”按钮之前不会初始化

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-09
        • 1970-01-01
        • 2021-08-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多