Issue
This is the code for controller
package Views;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.ResourceBundle;
import javax.imageio.ImageIO;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
public class menuController {
@FXML
private Label eNameLabel;
@FXML
private Button productListButton;
@FXML
private Button employeeListButton;
@FXML
private Button ingredientListButton;
@FXML
private Button addProductButton;
@FXML
private Button addIngreidentButton;
@FXML
private Button addEmployeeButton;
@FXML
private Button LogOutButton;
@FXML
private Label clockLabel;
public void initialize(URL arg0, ResourceBundle arg1) {
clock();
}
public void clock(){
Calendar cal = new GregorianCalendar();
// int day= cal.get(Calendar.DAY_OF_MONTH);
// int month = cal.get(Calendar.MONTH);
// int year = cal.get(Calendar.YEAR);
Date currentDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
// int second = cal.get(Calendar.SECOND);
// int minute = cal.get(Calendar.MINUTE);
// int hour = cal.get(Calendar.HOUR);
this.clockLabel.setText(dateFormat.format(currentDate));
}
}
My label is not changed when I start my program. I have checked that the label id is given in the scene-builder and everything is connected to its controller or variable.
This is the fxml file that is connected to the controller.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="764.0" prefWidth="901.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Views.menuController">
<children>
<AnchorPane layoutY="2.0" prefHeight="764.0" prefWidth="951.0">
<children>
<HBox prefHeight="100.0" prefWidth="746.0" spacing="20.0">
<children>
<Label lineSpacing="10.0" text="Welcom!">
<font>
<Font size="55.0" />
</font>
</Label>
<Label fx:id="eNameLabel" lineSpacing="10.0" prefHeight="81.0" prefWidth="244.0" text="Label">
<font>
<Font size="55.0" />
</font>
</Label>
</children>
</HBox>
<Button fx:id="LogOutButton" layoutX="768.0" layoutY="704.0" mnemonicParsing="false" onAction="#logOutButtonPushed" prefHeight="46.0" prefWidth="176.0" text="Log Out" />
<Button fx:id="employeeListButton" layoutX="74.0" layoutY="301.0" mnemonicParsing="false" prefHeight="81.0" prefWidth="215.0" text="Employee List" />
<Button fx:id="productListButton" layoutX="368.0" layoutY="301.0" mnemonicParsing="false" prefHeight="81.0" prefWidth="215.0" text="Products List" />
<Button fx:id="ingredientListButton" layoutX="661.0" layoutY="301.0" mnemonicParsing="false" prefHeight="81.0" prefWidth="215.0" text="Ingredients List" />
<Label fx:id="clockLabel" layoutX="74.0" layoutY="710.0" text="Clock" />
<Button fx:id="addEmployeeButton" layoutX="74.0" layoutY="439.0" mnemonicParsing="false" prefHeight="81.0" prefWidth="215.0" text="Add Employee" />
<Button fx:id="addProductButton" layoutX="368.0" layoutY="439.0" mnemonicParsing="false" prefHeight="81.0" prefWidth="215.0" text="Add Products" />
<Button fx:id="addIngreidentButton" layoutX="661.0" layoutY="439.0" mnemonicParsing="false" prefHeight="81.0" prefWidth="215.0" text="Add Ingredients" />
</children>
</AnchorPane>
</children>
</AnchorPane>
When I run the program, the clockLabel stays 'clock' and does not change. It also does not give me any error. When I put clock(); in a constructor I made later, it gave me a nullPointerException error at this.clockLabel.setText(dateFormat.format(currentDate));
I tried getting rid of this in this.clockLabel.setText(dateFormat.format(currentDate)); but it didnt work either.
Solution
An initialize
method taking parameters is only considered, if it's overriding the method from Initializable
. In your case the controller class does not implement Initializable
, so FXMLLoader
only looks for an initialize()
method. Either remove the parameters of the method or add implements Initializable
to the class.
As for the constructor resulting in a NPE: the injection of the fields happens after the constructor completes in the lifecycle of the controller:
FXMLLoader.load
is entered- Parsing the fxml the loader encounters the
fx:controller
attribute and uses reflection to create a instance of the controller. - The fields are injected based on their
fx:id
s - The
FXMLLoader.load
returns.
Answered By - fabian