NullPointerException与JButton操作侦听器

班迪:

我是编码的新手,我正在尝试制作基于文本,基于决策(因此称为1A变量和1B变量)的游戏。当我单击向西的JButton来唤起动作侦听器时,该程序将取消并显示此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
詹姆斯·安德(James Ander):

我看到您已经修复了代码,但是我想告诉您,您的类和变量的命名不符合Java约定。尽管这不会导致编译错误,但确实会使您的代码在其他Java开发人员中可读性降低,并且如果您想成为专业(java)开发人员,则绝对应该更改此内容。您现在在某些位置正确执行此操作,而在下一行错误地执行此操作。

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

有关所有Java约定的概述,请参见https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章