Sara
Sara

Reputation: 121

JavaFX DatePicker returning null

So I looked through questions and found this one, which was similar to my problem, but didn't address my issues as I haven't initialized my date pickers in my java file.

I created a date picker. Its fx:id in the fxml file matches its name in the corresponding controller. For some reason, whether I manually type a value or select a date from the calendar, datePicker.getValue() ALWAYS returns null. Can anyone think of why this might be happening?

My code is exactly as the situation I've described here. I have a DatePicker called lowEndDate. I have not initialized it. Upon the click of a button, I want to get its value, but it returns null. Does anyone have any idea why this might be? Perhaps I've missed a step. Thank you.

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<SplitPane dividerPositions="0.22122571001494767" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="395.0" prefWidth="671.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.UserMainController">
  <items>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="398.0" prefWidth="175.0">
         <children>
            <Button fx:id="logOut" layoutX="35.0" layoutY="261.0" mnemonicParsing="false" onAction="#logOutButtonClicked" text="Log Out">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Label layoutX="52.0" layoutY="44.0" text="user:">
               <font>
                  <Font size="19.0" />
               </font>
            </Label>
            <Label fx:id="username" layoutX="16.0" layoutY="76.0" prefHeight="32.0" prefWidth="116.0" textAlignment="CENTER">
               <font>
                  <Font size="25.0" />
               </font>
            </Label>
         </children></AnchorPane>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="442.0" prefWidth="457.0">
         <children>
            <ListView fx:id="albumListView" layoutY="-2.0" prefHeight="335.0" prefWidth="292.0" />
            <Button fx:id="deleteButton" layoutX="115.0" layoutY="348.0" mnemonicParsing="false" onAction="#deleteButtonClicked" prefHeight="31.0" prefWidth="68.0" text="Delete">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <AnchorPane layoutX="292.0" prefHeight="335.0" prefWidth="169.0">
               <children>
                  <Label fx:id="albums" layoutX="73.0" layoutY="44.0" text="Albums">
                     <font>
                        <Font size="22.0" />
                     </font>
                  </Label>
                  <Label fx:id="addSuccess" layoutX="99.0" layoutY="212.0" text="ADD SUCCESSFUL" textFill="#2bee34" visible="false" />
                  <Label fx:id="addFail" layoutX="81.0" layoutY="212.0" text="ALBUM EXISTS. TRY AGAIN" textFill="#d01313" visible="false" />
                  <AnchorPane fx:id="createAlbumAnchor" layoutY="7.0" prefHeight="335.0" prefWidth="169.0" visible="false">
                     <children>
                        <Label layoutX="46.0" layoutY="39.0" text="Create Album">
                           <font>
                              <Font size="22.0" />
                           </font>
                        </Label>
                        <Button fx:id="albumCreate" layoutX="46.0" layoutY="229.0" mnemonicParsing="false" onAction="#OKButtonClicked" text="OK">
                           <font>
                              <Font size="15.0" />
                           </font>
                        </Button>
                        <Button fx:id="back" layoutX="121.0" layoutY="229.0" mnemonicParsing="false" onAction="#backButtonClicked" text="Back">
                           <font>
                              <Font size="15.0" />
                           </font>
                        </Button>
                        <TextField fx:id="newAlbumName" layoutX="38.0" layoutY="115.0" promptText="Album name" />
                        <Label fx:id="mustInputText" layoutX="64.0" layoutY="160.0" text="MUST INPUT TEXT" textFill="#cd1515" visible="false" />
                     </children>
                  </AnchorPane>
                  <AnchorPane fx:id="searchAnchor" layoutX="4.0" layoutY="7.0" prefHeight="321.0" prefWidth="212.0">
                     <children>
                        <Label layoutX="74.0" layoutY="35.0" text="Search">
                           <font>
                              <Font size="22.0" />
                           </font>
                        </Label>
                        <Label layoutX="76.0" layoutY="67.0" text="Date Range" />
                        <Label layoutX="91.0" layoutY="123.0" text="to" />
                        <Label layoutX="91.0" layoutY="185.0" text="Tags" />
                        <TextArea fx:id="tags" layoutX="24.0" layoutY="208.0" prefHeight="71.0" prefWidth="166.0" promptText="ex: healthy,happy,nyc" />
                        <Button fx:id="searchOK" layoutX="48.0" layoutY="282.0" mnemonicParsing="false" onAction="#searchOKButtonClicked" text="OK" />
                        <Button fx:id="searchBack" layoutX="118.0" layoutY="282.0" mnemonicParsing="false" onAction="#searchBackButtonClicked" text="Back" />
                        <DatePicker fx:id="highEndDate" layoutX="20.0" layoutY="92.0" promptText="dd/mm/yyyy" />**
                        <DatePicker fx:id="lowEndDate" layoutX="20.0" layoutY="148.0" promptText="dd/mm/yyyy" />
                     </children>
                  </AnchorPane>
               </children>
            </AnchorPane>
            <Button fx:id="createButton" layoutX="21.0" layoutY="348.0" mnemonicParsing="false" onAction="#createButtonClicked" text="Create">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button fx:id="searchButton" layoutX="212.0" layoutY="348.0" mnemonicParsing="false" onAction="#searchButtonClicked" text="Search">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
         </children></AnchorPane>
  </items>
</SplitPane>

Code:

public void searchOKButtonClicked(ActionEvent e){
        boolean searchComplete = false;
        User currentUser = LoginController.currentUser;
        if((lowEndDate.getValue() == null) && (highEndDate.getValue() == null) && ((tags == null) || tags.getText().trim().isEmpty())){
            Alert alert = new Alert(AlertType.ERROR);
            alert.setContentText("Oops! At least one field is required to search.");
            alert.show();
        }

}

The trouble here: lowEndDate and highEndDate are ALWAYS null, regardless of input.

Upvotes: 1

Views: 1603

Answers (1)

RubioRic
RubioRic

Reputation: 2468

Sara, I've tested your fxml file using this simple project

Main

  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;

  public class Main extends Application {
     @Override
     public void start(Stage primaryStage) throws Exception{
         Parent root = FXMLLoader.load(getClass().getResource("sample2.fxml"));
         primaryStage.setTitle("Hello World");
         primaryStage.setScene(new Scene(root));
         primaryStage.show();
     }

     public static void main(String[] args) {
        launch(args);
     }
  }

Controller

   import java.net.URL;
   import java.util.ResourceBundle;

   import javafx.event.ActionEvent;
   import javafx.fxml.FXML;
   import javafx.fxml.Initializable;
   import javafx.scene.control.Alert;
   import javafx.scene.control.Alert.AlertType;
   import javafx.scene.control.DatePicker;

   public class Controller implements Initializable {

       @FXML
       DatePicker lowEndDate;

       public void searchOKButtonClicked(ActionEvent e){

            Alert alert = new Alert(AlertType.ERROR);
            alert.setContentText("Oops! At least one field is required to search. " + " " + lowEndDate.getValue());
            alert.show();
       }

       @Override
       public void initialize(URL arg0, ResourceBundle arg1) {
           // TODO Auto-generated method stub

       }
   }

In your fxml, I've changed the naming in the two datepickers, I think that lowEndDate must be above highEndDate

  <DatePicker fx:id="lowEndDate"  layoutX="20.0" layoutY="92.0"  promptText="dd/mm/yyyy" />
  <DatePicker fx:id="highEndDate" layoutX="20.0" layoutY="148.0" promptText="dd/mm/yyyy" />

This is the result that I've obtained when I introduce a date

enter image description here

enter image description here

Maybe you have an error in another part of your controller but the datepickers seem to work fine.

Upvotes: 1

Related Questions