How to get the selected date from the DatePicker ?

Part of fxml code:

 <children> <Button fx:id="showLast" mnemonicParsing="false" prefHeight="23.0" prefWidth="200.0" text="Show last 10" GridPane.rowIndex="3" /> <Button fx:id="showAll" mnemonicParsing="false" prefHeight="23.0" prefWidth="200.0" text="Show all" GridPane.rowIndex="2" /> <Button fx:id= "showBetweenDates" mnemonicParsing="false" prefWidth="199.0" text="Show Between Dates" /> <DatePicker fx:id= "endDate" prefWidth="187.0" GridPane.columnIndex="2" GridPane.rowIndex="1" /> <DatePicker fx:id= "startDate" onAction="#startDatePickerAction" prefWidth="186.0" GridPane.columnIndex="2" /> <Label prefHeight="17.0" prefWidth="57.0" text="Start Date" GridPane.columnIndex="1" /> <Label prefHeight="17.0" prefWidth="53.0" text="End Date" GridPane.columnIndex="1" GridPane.rowIndex="1" /> </children> 

and my controller code is:

 public class HistoryController implements Initializable { private final SalesSystemDAO dao; private LocalDate start; private LocalDate end; private static final Logger log = LogManager.getLogger(HistoryController.class); @FXML private Button showBetweenDates; @FXML private DatePicker startDatePicker; @FXML private DatePicker endDatePicker; @FXML private TableView<SoldItem> purchaseHistoryTableView; public HistoryController(SalesSystemDAO dao) { this.dao = dao; } @FXML private void startDatePickerAction() { start = startDatePicker.getValue(); // тут выдаёт ошибку } @Override public void initialize(URL location, ResourceBundle resources) { log.info("History initializing"); // TODO: implement } } 
  • тут выдаёт ошибку - which one? - post_zeew pm
  • Exception in thread error "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException, occurs when choosing a number. Before that, everything works fine. If you change startDatePicker.getValue () to any other DatePicker method, the error is the same. - tuleviku6
  • one
    Doesn’t the name of an annotated variable coincide with fx:id ? In fxml , you have a DatePicker fx:id= "startDate" , and in the controller, a private DatePicker startDatePicker; . Try fxml replace fx:id= "startDate" with fx:id= "startDatePicker" . - post_zeew
  • Yeah))) now everything works! Thanks - tuleviku6

1 answer 1

The names of the annotated variables must match fx:id in the fxml file.

In fxml replace <DatePicker fx:id= "startDate" with <DatePicker fx:id= "startDatePicker" .

Well and with other components similarly.