Java 在LocalDate构造函数中将日期选择器值用作参数时出错

Java 在LocalDate构造函数中将日期选择器值用作参数时出错,java,javafx,datepicker,Java,Javafx,Datepicker,我使用DatePicker获取LocalDate,然后将其转换为字符串。 但每次我试图从DatePicker获取年、月和日,然后将它们用作LocalDate构造函数中的参数。程序崩溃了。如果我只是在LocalDate的构造函数中手动输入年份、月份和日期,那么它在TableView中就可以正常工作 @Override public void initialize(URL url, ResourceBundle rb) { roomTypeChoice.setValue("Select")

我使用DatePicker获取LocalDate,然后将其转换为字符串。 但每次我试图从DatePicker获取年、月和日,然后将它们用作LocalDate构造函数中的参数。程序崩溃了。如果我只是在LocalDate的构造函数中手动输入年份、月份和日期,那么它在TableView中就可以正常工作

@Override
public void initialize(URL url, ResourceBundle rb) 
{
    roomTypeChoice.setValue("Select");
    roomTypeChoice.setItems(roomTypeList);

    //setting the cell of each Column that propertyValueFactory should make sure that 
    //the variable name should be consisted with memeber variable name otherwise the tableview can not 
    //display the value
    firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
    lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
    checkInCol.setCellValueFactory(new PropertyValueFactory<>("checkInDate"));
    checkOutCol.setCellValueFactory(new PropertyValueFactory<>("checkOutDate"));      
    phoneCol.setCellValueFactory(new PropertyValueFactory<>("phoneNumber"));
    addressCol.setCellValueFactory(new PropertyValueFactory<>("address"));
    emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));



}    

@FXML
private void checkIn(ActionEvent event)  
{

    //get the Check in date convert to Date Object from DatePicker
    LocalDate checkInLocalDate=LocalDate.of(checkInDatePicker.getValue().getYear(), checkInDatePicker.getValue().getMonthValue(), checkInDatePicker.getValue().getDayOfMonth());
    String checkInDate=checkInLocalDate.toString();
    System.out.println(checkInLocalDate.toString()+"");
    Customer customer=new Customer(firstNameTextField.getText().trim(),lastNameTextField.getText().trim(),checkInDate,checkInDate
    ,phoneTextField.getText().trim(),addressTextField.getText().trim(),emailTextField.getText().trim());


    tableView.getItems().add(customer);



}
@覆盖
公共void初始化(URL、ResourceBundle rb)
{
roomTypeChoice.setValue(“选择”);
roomTypeChoice.setItems(roomTypeList);
//设置propertyValueFactory应确保的每列的单元格
//变量名应与memeber变量名一致,否则tableview无法
//显示值
firstNameCol.setCellValueFactory(新属性ValueFactory(“firstName”));
lastNameCol.setCellValueFactory(新属性ValueFactory(“lastName”));
checkInCol.setCellValueFactory(新属性ValueFactory(“checkInDate”);
checkOutCol.setCellValueFactory(新属性ValueFactory(“checkOutDate”);
phoneCol.setCellValueFactory(新属性ValueFactory(“phoneNumber”);
addressCol.setCellValueFactory(新属性ValueFactory(“地址”);
emailCol.setCellValueFactory(新属性ValueFactory(“电子邮件”);
}    
@FXML
私有无效签入(ActionEvent事件)
{
//从DatePicker获取签入日期转换为日期对象
LocalDate checkInLocalDate=LocalDate.of(checkInDatePicker.getValue().getYear(),checkInDatePicker.getValue().getMonthValue(),checkInDatePicker.getValue().getDayOfMonth());
字符串checkInDate=checkInLocalDate.toString();
System.out.println(checkInLocalDate.toString()+);
客户=新客户(firstNameTextField.getText().trim(),lastNameTextField.getText().trim(),checkInDate,checkInDate
,phoneTextField.getText().trim(),addressTextField.getText().trim(),emailTextField.getText().trim());
tableView.getItems().add(客户);
}
我希望能摆脱这个让我非常头疼的问题。

试试这个

LocalDate localDate = checkInDatePicker.getValue();

int day = localDate.getDayOfMonth();
int month = localDate.getMonthValue();
int year = localDate.getYear();

我可以问一下为什么要从另一个的值创建
LocalDate
LocalDate
的实例是不可变的,因此不需要复制。你能提供一个例子来说明这个问题吗?在问题中也包括异常的堆栈跟踪。我无法使用OpenJDK 12和JavaFX 12重现这个问题,尽管如果没有实际的错误,我不确定问题是什么。