2

There is my main class:

public class Main extends Application {
private static Stage primaryStage;
public static BorderPane mainLayout;

@Override
public void start(Stage primaryStage) {
    this.setPrimaryStage(primaryStage);
    primaryStage.setTitle("Project");

    try {
        mainLayout = 
        FXMLLoader.load(Main.class.getResource("/main/view/MainPage.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(mainLayout);
        primaryStage.setScene(scene);
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {

            @Override
            public void handle(WindowEvent event) {
                System.exit(0);
            }
        });
        primaryStage.show();
    }

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

    public static Stage getPrimaryStage() {
        return primaryStage;
    }

    public void setPrimaryStage(Stage primaryStage) {
         Main.primaryStage = primaryStage;
    }
}

This FXML of my window:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<BorderPane prefHeight="410.0" prefWidth="512.0" 
xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="main.controller.MainController">
 <center>
  <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" 
  spacing="20.0" BorderPane.alignment="CENTER">
     <children>
        <Label fx:id="aaa" prefHeight="72.0" prefWidth="336.0" 
text="Project" textAlignment="CENTER">
           <font>
              <Font name="Century Gothic" size="25.0" />
           </font>
        </Label>
     </children>
     <padding>
        <Insets bottom="30.0" />
     </padding>
   </VBox>
 </center>
</BorderPane>

This is Controller for this FXML:

public class MainController {
@FXML
private static Label aaa;

@FXML
public static void initialize(){
    aaa.setText("AHOJ");

    }
} 

I want to call method initialize() from another class ten times like this:

public class MyClass {
public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
        MainController.initialize();
    }
}
}

But there is NullPointerException. Can someone help me?

M. Barabas
  • 53
  • 1
  • 2
  • 11
  • You got NPE because your static field is not initialized. What is the final goal of your for loop? – Dmytro Maslenko Jul 14 '17 at 20:11
  • It is example. My goal is to update Label text more then just one time by calling method from another class but I dont know how. – M. Barabas Jul 14 '17 at 20:14
  • What is other class? This is a FX application, you run via main() method and it runs in separate thread. From which class are you planning to change a label? – Dmytro Maslenko Jul 14 '17 at 20:25
  • From class `MyClass` as it is in example. I need call some method or something to change label text. – M. Barabas Jul 14 '17 at 20:31

1 Answers1

4

Just remove statics for field and method, then run your application by main() in Main class:

public class MainController {
    @FXML
    private Label aaa;

    @FXML
    public void initialize(){
        aaa.setText("AHOJ");
    }
}
Dmytro Maslenko
  • 2,247
  • 9
  • 16
  • I know that it works, but I want to call method initialize() from another class. Or I want to find any other way, how to update my Label text from another class more times. Every time other word for example. – M. Barabas Jul 14 '17 at 20:29
  • It is impossible to run both main() from Main and from MyClass and interact between each other. You may change a label only from FX application as reaction on user action or from background job. Say, an user does something and as result a label changed. – Dmytro Maslenko Jul 14 '17 at 20:32
  • Your controller should be used to update or "control" the items on the FXML that they represent. If you want to invoke an update from somewhere else you can look at links on how to do just that. Create a method on the controller... https://stackoverflow.com/questions/33237356/access-controller-in-another-controller-class – purring pigeon Jul 14 '17 at 21:43