Exception 调用方法时出现空指针异常错误

Exception 调用方法时出现空指针异常错误,exception,pointers,null,Exception,Pointers,Null,我不知道我在做什么有问题。它应该打印在特定日期就诊的所有患者,但它一直抛出空指针错误。当我调用printPatientsOnDate方法时就会发生这种情况 主/UI类上的代码 public void printPatientsOnDate() throws ParseException { System.out.print("Enter the date(mm-dd-yyyy): "); Date dt = new SimpleDateFormat("MM-dd-yyyy").p

我不知道我在做什么有问题。它应该打印在特定日期就诊的所有患者,但它一直抛出空指针错误。当我调用printPatientsOnDate方法时就会发生这种情况

主/UI类上的代码

public void printPatientsOnDate() throws ParseException
{
    System.out.print("Enter the date(mm-dd-yyyy): ");
    Date dt = new SimpleDateFormat("MM-dd-yyyy").parse(sc.nextLine());
    for(Patient i : app.getPatientsOnSpecDate(dt))
    {
    System.out.println(i.getName());
    }
}
关于临床课的代码

public ArrayList<Patient> getPatientsOnSpecDate(Date date)
{
    ArrayList<Patient> patients = null;
    for(Patient i : patientList)
    {
      if(i.searchDates(date)!=null)
      {
        patients.add(i);
      }
    }
    return patients;
}
您的
ArrayList patients
参考变量为
null
。目前它没有指向任何
ArrayList
对象

ArrayList<Patient> patients = null;
或者最好使用
列表
作为参考类型:

List<Patient> patients = new ArrayList<Patient>(); 
列出患者=新建ArrayList();

非常感谢!错误没有弹出,但我有另一个问题。printPatientsOnDate在调用时不打印任何患者。for-each循环有什么问题吗?向我们展示正在运行的代码,可能会用该代码提出一个新问题。我们不能只使用两种随机方法进行调试!
ArrayList<Patient> patients = new ArrayList<Patient>();
List<Patient> patients = new ArrayList<Patient>();