Java program bug due to multiple declaration of variable in code and the fix

I created a small Java class to accept a date. However, when I tried to use it, I got a NullPointerException error. This post is just to illustrate how it was fixed.

1  public class DateForm {
2     JFormattedTextField txtMyDate;                //This is a variable at class level
4     dateForm () {
5        JTextFormattedField txtMyDate = new JTextFormattedTextField(new Date());
6        System.out.println(txtMyDate.getText();    //This works
7     }
9     validateForm() {
10       System.out.println(txtMyDate.getText());   //This returns NullPointerException.
11    }
12  }

because, the txtMyDate field that was initialized was within dateForm().

To make it work correctly, remove JTextFormattedField before the txtMyDate variable in the dateForm() method in line number 5.

To compare dates, refer I found this to be one of the most simplest ways it was explained.