I am unable to change the text of my label from other class. I need to constantly update date and time on the main screen, while I may be able to perform other functions too, simultaneously. I have used a TimeSetting class, which extends Thread and in it's run() method, I've called the updation command in an infinite loop, using setText() and then slept the method for a second. But on running this, nothing happens and on closing the output window, I get an error saying NullPointerExcpetion
Here's the code for the two classes : FXMLDocumentController.java
package crt;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class FXMLDocumentController extends Thread implements Initializable
{
@FXML
protected Label check;
@FXML
**protected Label date;**
@FXML
protected Label time;
@FXML
protected Label RRRR;
@FXML
protected Label DDDD;
@FXML
protected Label SSSS;
@FXML
protected Label temp;
@FXML
protected Label maxtemp;
@FXML
protected Label mintemp;
@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
//dc.setDate(date.textProperty().bind(valueproperty));
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("menu.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.initStyle(StageStyle.UNDECORATED);
stage.setTitle("MENU");
stage.setScene(new Scene(root1));
stage.show();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="367.0" prefWidth="510.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="crt.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="387.0" layoutY="302.0" minHeight="25.0" minWidth="80.0" onAction="#handleButtonAction" onTouchPressed="#handleButtonAction" text="Menu" />
<Label fx:id="date" layoutX="56.0" layoutY="64.0" minHeight="25.0" minWidth="80.0" />
<Label fx:id="time" layoutX="361.0" layoutY="64.0" minHeight="25.0" minWidth="80.0" text="S" />
<Label fx:id="RRRR" layoutX="76.0" layoutY="100.0" minHeight="25.0" minWidth="70.0" />
<Label fx:id="DDDD" layoutX="195.0" layoutY="100.0" minHeight="25.0" minWidth="70.0" />
<Label fx:id="SSSS" layoutX="314.0" layoutY="100.0" minHeight="25.0" minWidth="70.0" />
<Text layoutX="136.0" layoutY="163.0" strokeType="OUTSIDE" strokeWidth="0.0" text="TEMP :-" />
<Label fx:id="temp" layoutX="275.0" layoutY="156.0" minHeight="25.0" minWidth="70.0" text="A" />
<Text layoutX="136.0" layoutY="203.0" strokeType="OUTSIDE" strokeWidth="0.0" text="MAX TEMP :-" />
<Label fx:id="maxtemp" layoutX="275.0" layoutY="188.0" minHeight="25.0" minWidth="70.0" text="B" />
<Text layoutX="136.0" layoutY="243.0" strokeType="OUTSIDE" strokeWidth="0.0" text="MIN TEMP :-" />
<Label fx:id="maxtemp" layoutX="275.0" layoutY="225.0" minHeight="25.0" minWidth="70.0" text="C" />
<ProgressBar layoutX="401.0" layoutY="21.0" prefHeight="18.0" prefWidth="70.0" progress="0.0" />
<Button fx:id="startbutton" layoutX="14.0" layoutY="18.0" mnemonicParsing="false" onAction="#startstart" text="START" />
<Label fx:id="check" layoutX="42.0" layoutY="306.0" />
</children>
</AnchorPane>
TimeSetting.java
package crt;
import java.util.Date;
public class TimeSetting extends Thread {
@Override
public void run()
{
FXMLDocumentController fdc = new FXMLDocumentController();
// fdc.load();
int i=0;
while(true)
{
Date d = new Date();
fdc.date.setText("fd" + i);
i++;
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
}
}
}
}
CRT.java
package crt;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class CRT extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) throws InterruptedException {
launch(args);
TimeSetting ts = new TimeSetting();
ts.start();
}
}