Javafx 测试日期选择器是否有值

Javafx 测试日期选择器是否有值,javafx,javafx-2,Javafx,Javafx 2,我有两个数据采集器,d1和d2。 有一个过滤功能,该功能应仅通过d1、d2或同时通过d1和d2进行过滤 @FXML DatePicker d1; @FXML DatePicker d2; public void filter(){ SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd"); String st = ft.format(ft.parse(this.d1.getValue().toString()))); Str

我有两个数据采集器,d1和d2。 有一个过滤功能,该功能应仅通过d1、d2或同时通过d1和d2进行过滤

@FXML
DatePicker d1;
@FXML
DatePicker d2;

public void filter(){
  SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");

  String st = ft.format(ft.parse(this.d1.getValue().toString())));
  String en = ft.format(ft.parse(this.d2.getValue().toString())));

  if(st == null && en != null)
    filterbySt(d1);
  else
    filterbyEn(d2);
}
如果未设置d1或d2,则会出现空指针异常

如何测试是否未设置日期选择器?

问题 如果未设置d1/d2 ae,
d1.getValue()
返回
null
,使用
d1.getValue().toString()
将抛出
NullPointerException

解决方案 在尝试从日期选择器中获取值之前,只需执行一个
null检查。如果
datePicker.getValue()
null
,则表示尚未设置日期选择器

问题 如果未设置d1/d2 ae,
d1.getValue()
返回
null
,使用
d1.getValue().toString()
将抛出
NullPointerException

解决方案 在尝试从日期选择器中获取值之前,只需执行一个
null检查。如果
datePicker.getValue()
null
,则表示尚未设置日期选择器

问题 如果未设置d1/d2 ae,
d1.getValue()
返回
null
,使用
d1.getValue().toString()
将抛出
NullPointerException

解决方案 在尝试从日期选择器中获取值之前,只需执行一个
null检查。如果
datePicker.getValue()
null
,则表示尚未设置日期选择器

问题 如果未设置d1/d2 ae,
d1.getValue()
返回
null
,使用
d1.getValue().toString()
将抛出
NullPointerException

解决方案 在尝试从日期选择器中获取值之前,只需执行一个
null检查。如果
datePicker.getValue()
null
,则表示尚未设置日期选择器


我还尝试过像这样捕获null指针异常

  //catching the null pointer exception and moving on.
    String st = null, en = null;
    try{
        st = ft.format(ft.parse(this.d1.getValue().toString())));
    }catch(NullPointerException ex) {}

    try{
         en = ft.format(ft.parse(this.d2.getValue().toString())));
    }catch(NullPointerException ex){}

我还尝试过像这样捕获null指针异常

  //catching the null pointer exception and moving on.
    String st = null, en = null;
    try{
        st = ft.format(ft.parse(this.d1.getValue().toString())));
    }catch(NullPointerException ex) {}

    try{
         en = ft.format(ft.parse(this.d2.getValue().toString())));
    }catch(NullPointerException ex){}

我还尝试过像这样捕获null指针异常

  //catching the null pointer exception and moving on.
    String st = null, en = null;
    try{
        st = ft.format(ft.parse(this.d1.getValue().toString())));
    }catch(NullPointerException ex) {}

    try{
         en = ft.format(ft.parse(this.d2.getValue().toString())));
    }catch(NullPointerException ex){}

我还尝试过像这样捕获null指针异常

  //catching the null pointer exception and moving on.
    String st = null, en = null;
    try{
        st = ft.format(ft.parse(this.d1.getValue().toString())));
    }catch(NullPointerException ex) {}

    try{
         en = ft.format(ft.parse(this.d2.getValue().toString())));
    }catch(NullPointerException ex){}