[Solved] java.lang.NullPointerException on method


Problem is in the actionPerformed() method. The class variable patient is null.

You can do a null check like this…

public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == reportButton && patient != null)
    {
        System.out.println("I'm Clicked!");
        patient.setAge(ageField, log);
    }
}

Or you can initalize the variable…

public void actionPerformed(ActionEvent e)
{
    if (patient == null)
    {
        patient = new Patient();
    }

    if(e.getSource() == reportButton)
    {
        System.out.println("I'm Clicked!");
        patient.setAge(ageField, log);
    }
}

Or you initalize the variable when you declare it…

private Patient patient = new Patient();

3

solved java.lang.NullPointerException on method