Printing a string in a text box using FXML

  Kiến thức lập trình

Im making a weather app in which I am using a weather controller along side scenebuilder to controll actions within the app. I am trying to make a text box display the current temp which I have set to 20 degrees however it is saying that it is null and will not run

I tried making an initialize method and running current temp through it however it says that is the problem
package application;
import java.io.IOException;

 import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Node;
  import javafx.scene.Parent;
   import javafx.scene.Scene;
  import javafx.scene.text.Text;
  import javafx.stage.Stage;

 public class weatherController {
private Stage stage;
private Scene scene;
private Parent root;

@FXML 
private Text currentTempText;

public void switchToWeather(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Weather.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}
public void switchToSunday(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Sunday.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}
public void switchToMonday(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Monday.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}
public void switchToTuesday(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Tuesday.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}
public void switchToWednesday(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Wednesday.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}
public void switchToThursday(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Thursday.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}
public void switchToFriday(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Friday.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}
public void switchToSaturday(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Saturday.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
 }
private String currentTemp = "20°C";

public void initialize() {
    currentTempText.setText(currentTemp);
}

 }

it says the last line returns null and I dont understand how to fix that

New contributor

Christian Bekheit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT