I get this strange error when I try to extract data from a text field on focusLost.
So I have a text-field and the idea is that when user enters data after that clicks somewhere else, the input has to be validated.
this is the line in constructor where I add ficus listener to the text field
ip_address_textField.addFocusListener(new ip_address_textField_FieldHandler());
inside of this class I have another class
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 ->");
}
The JTextField is class variable
The error which I get is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at mia.recommender.ch02.Course_Recommender$ip_address_textField_FieldHandler.focusLost
Either of these two is null :
ip_address_textField
- While you try to call getText() it throws nullpointerip_address_textField.getText()
- While you try to trim it throws nullpointer