簡體   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