当我尝试从 focusLost 上的文本字段中提取数据时,我收到了这个奇怪的错误。
所以我有一个文本字段,想法是当用户在点击其他地方后输入数据时,必须验证输入。
这是我在文本字段中添加榕侦听器的构造函数中的行
ip_address_textField.addFocusListener(new ip_address_textField_FieldHandler());
在这堂课里面我有另一堂课
public class ip_address_textField_FieldHandler implements FocusListener {
private static final String IPADDRESS_PATTERN =
"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
private Pattern pattern;
private Matcher matcher;
@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
System.out.println("Focus Gained");
}
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
System.out.println(ip_address_textField.getText().trim());
//ip_address_textField.setText(IPADDRESS_PATTERN);
System.out.println("Focus Lost ->");
}
JTextField 是类变量
我得到的错误是:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at mia.recommender.ch02.Course_Recommender$ip_address_textField_FieldHandler.focusLost