A java.lang.NullPointerException error occurs when trying to set text in a label and read it from a label.

 package application; import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.FXML; import javafx.scene.layout.AnchorPane; import javafx.scene.control.Button; import javafx.scene.control.Label; public class DialogConroller { @FXML private static Button btn_savefile; @FXML private static Label label; @FXML private ResourceBundle resources; @FXML private URL location; @FXML private AnchorPane paneAnch; @FXML void initialize() { paneAnch.setMaxSize(100, 300); System.out.println(label.getText().toString()); // Platform.runLater((() -> label.setText(""))); } } 

There were also attempts to shove in a separate stream, but it led to nothing.

  • one
    Show how you create DialogConroller. This should do fxmlLoader and why static variables - Maxim
  • FXMLLoader load = new FXMLLoader(); load.setLocation(getClass().getResource("/application/dialog.fxml")); try { load.load(); } catch (IOException e) { e.printStackTrace(); } Stage stage = new Stage(); Parent root = load.getRoot(); Scene scene = new Scene(root, 300,100); stage.setScene(scene); stage.setResizable(false); stage.showAndWait(); The static variable, since created automatically via SceneBuilder - Bagul
  • one
    remove statics from variables and check that fxml is indicated inside fx: id = "label". And Platform.Later is not needed here, you are in the FX stream in this method. - Maxim
  • I removed the static and everything turned out. Now the question is, why is it so static? Thanks for the help. - Bagul
  • one
    I don’t know, but IDEA removes the icon representing the connectivity with the .fxml file if it is static. - Maxim

1 answer 1

FXMLLoader does not FXMLLoader data in variables declared static . It is necessary to remove static from variables.

ResourceBundle and Location , if not used, can be removed. Also, the initialize() call produces fxmlolder inside the FX stream, so Platform.runLater() is superfluous here.