繁体   English   中英

基本textField actionListener发生错误(命令在侦听器中时发生错误)

[英]Error with basic textField actionListener (Error when commands are in the listener)

当我在文本字段上按Enter时,我一直收到错误消息(以下将发帖错误)。 我希望文本字段将数据保存到全局定义的变量。 每当我的代码中不包含“名称”时,actionListener都可以工作,例如,如果我将int a = 3放入,则没有错误。 我也在全局上(在主gui上)声明了名称,因为如果我没有收到错误消息,指出变量不在范围内,也许这是一个问题吗?

    //Declared inside the main gui (the others are nested in this)
    JTextField name;
    JLabel nameLabel;

    //Name text field defined inside the gui jInternalFrame
    TextField name = new TextField("Enter Name..", 20);
    JLabel nameLabel = new JLabel();
    nameLabel.setText("Name: ");
    name.addActionListener(new nameListener());
    addRoomPanel.add(nameLabel);
    addRoomPanel.add(name);`

    //ActionListener defined outside of the text field gui
    class nameListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            nameString = name.getText();
            name.setText("saved");
            name.selectAll();
         }
    }

ERROR MESSAGE:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at InternalFrame$dobListener.actionPerformed(InternalFrame.java:445)
    at java.awt.TextField.processActionEvent(TextField.java:617)
    at java.awt.TextField.processEvent(TextField.java:585)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
    at java.awt.EventQueue.access$300(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:706)
    at java.awt.EventQueue$3.run(EventQueue.java:704)
    at java.security.AccessController.doPrivileged(Native Method)
    at  java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.awt.EventQueue$4.run(EventQueue.java:718)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

UI的组件定义了两次。 一次在GUI类中作为变量,一次在(我想)构造函数中。 由于这个nameLabel存在名称为namenameLabel两个变量。 构造函数将访问在构造函数中声明的变量,因此GUI类的变量保持未初始化( null )。 ActionListener访问GUI类中的变量,该变量为null并抛出NullPointerException 您必须使用一个变量而不是两个。 为了获得更准确的答案,我需要更多代码(或者至少比上面发布的摘录更有用)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM