基本 textField actionListener 出错(命令在侦听器中时出错)
Error with basic textField actionListener (Error when commands are in the listener)
当我在我的文本字段上按回车键时,我不断收到错误消息(下面会出现 post 错误)。我希望文本字段将数据保存到全局定义的变量中。每当我的代码中不包含 'name' 时,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.access0(EventQueue.java:103)
at java.awt.EventQueue.run(EventQueue.java:706)
at java.awt.EventQueue.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue.run(EventQueue.java:720)
at java.awt.EventQueue.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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-class 作为变量,一次在(我想)构造函数中。因此存在名称为 name
和 nameLabel
的两个变量。构造函数将访问在构造函数中声明的变量,因此 GUI-class 的变量保持未初始化状态 (null
)。 ActionListener
访问 GUI-class 中的变量,即 null
并抛出一个 NullPointerException
。您必须使用一个变量而不是两个。为了获得更准确的答案,我需要更多的代码(或者至少比上面发布的代码片段更有用)。
当我在我的文本字段上按回车键时,我不断收到错误消息(下面会出现 post 错误)。我希望文本字段将数据保存到全局定义的变量中。每当我的代码中不包含 'name' 时,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.access0(EventQueue.java:103)
at java.awt.EventQueue.run(EventQueue.java:706)
at java.awt.EventQueue.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue.run(EventQueue.java:720)
at java.awt.EventQueue.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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-class 作为变量,一次在(我想)构造函数中。因此存在名称为 name
和 nameLabel
的两个变量。构造函数将访问在构造函数中声明的变量,因此 GUI-class 的变量保持未初始化状态 (null
)。 ActionListener
访问 GUI-class 中的变量,即 null
并抛出一个 NullPointerException
。您必须使用一个变量而不是两个。为了获得更准确的答案,我需要更多的代码(或者至少比上面发布的代码片段更有用)。