简体   繁体   English

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

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

I keep getting an error (Will post error below) When I press enter on my text field. 当我在文本字段上按Enter时,我一直收到错误消息(以下将发帖错误)。 I want the text field to save the data to a globally defined variable. 我希望文本字段将数据保存到全局定义的变量。 The actionListener works whenever I don't include 'name' in my code, for example if I put int a = 3 then there are no errors. 每当我的代码中不包含“名称”时,actionListener都可以工作,例如,如果我将int a = 3放入,则没有错误。 I have also declared name globally (on the main gui) because if I don't I get an error saying the variable is not in the scope, maybe this is an issue? 我也在全局上(在主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)

The Components of the UI are defined twice. UI的组件定义了两次。 Once in the GUI-class as variables, once in the (i suppose) constructor. 一次在GUI类中作为变量,一次在(我想)构造函数中。 Due to this two variables with name name and nameLabel exist. 由于这个nameLabel存在名称为namenameLabel两个变量。 The constructor will access the ones declared in the constructor, thus the variables of the GUI-class remain uninitialized ( null ). 构造函数将访问在构造函数中声明的变量,因此GUI类的变量保持未初始化( null )。 The ActionListener access the variable in the GUI-class, which is null and throws a NullPointerException . ActionListener访问GUI类中的变量,该变量为null并抛出NullPointerException You'll have to use one variable instead of two. 您必须使用一个变量而不是两个。 For a more precise answer, i'll need a bit more code (or atleast something more useable than the snippets posted above). 为了获得更准确的答案,我需要更多代码(或者至少比上面发布的摘录更有用)。

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

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