Ich bekomme eine NullPointerException in JavaFX beim Initialisieren meiner FXML in Main-Klasse.NullPointerException in JavaFX beim Initialisieren von drei Controller-Klassen in Hauptfunktion
Ich rufe in jeder Controller-Klasse die Main-Funktion initProgramm() auf und zähle die Aufrufe.
@Override
public void initialize(URL url, ResourceBundle rb) {
Main.initProgramm();
}
Hauptklasse
public class Main extends Application {
public static Stage stage;
public static String language;
public static ControllerMain controllerMain;
public static ControllerTabManual controllerTabManual;
public static ControllerTabCode controllerTabCode;
private static int countInits=0;
@Override
public void start(Stage stage) throws Exception {
try(Scanner file = new Scanner(new FileInputStream(new File("res/settings/language.txt")), StandardCharsets.UTF_8.name())) {
language = file.nextLine().trim();
}
catch(Exception e){}
FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml/Main.fxml"));
Parent root = (Parent) loader.load();
controllerMain = (ControllerMain) loader.getController();
controllerTabManual = (ControllerTabManual) loader.getController();
controllerTabCode = (ControllerTabCode) loader.getController();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("css/default.css").toExternalForm());
stage.getIcons().addAll(
new Image(getClass().getResourceAsStream("img/app_icon64x64.png")),
new Image(getClass().getResourceAsStream("img/app_icon32x32.png")),
new Image(getClass().getResourceAsStream("img/app_icon16x16.png"))
);
stage.setTitle("stepControl");
stage.setScene(scene);
stage.setOnCloseRequest(e -> {
e.consume();
closeProgramm();
}
);
stage.show();
Main.stage = stage;
}
public static void initProgramm() {
countInits++;
if(countInits==3)
{
if(Main.language.equals("de"))
controllerMain.menuLanguageOne.setDisable(true); // LINE 71
else if(Main.language.equals("en"))
controllerMain.menuLanguageTwo.setDisable(true);
controllerMain.grblSender = new GrblSender();
controllerMain.grblSender.setPorts(controllerTabManual.comboBoxSelectPort, true);
}
}
public static void main(String[] args) {
launch(args);
}
}
Letzter Fehler:
Caused by: java.lang.NullPointerException
at de.emericon.stepcontrol.Main.initProgramm(Main.java:71)
at de.emericon.stepcontrol.ControllerMain.initialize(ControllerMain.java:55)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 12 more
Exception running application de.emericon.stepcontrol.Main
Ich denke, die Ursache für die Ausnahme ist die Referenzierung der Controller-Klassen ?!
public static ControllerMain controllerMain;
public static ControllerTabManual controllerTabManual;
public static ControllerTabCode controllerTabCode;
Zunächst einmal: Eine FXML-Datei soll einen Controller haben. Der FXML Loader erstellt einen Controller, auf den Sie in der FXML-Datei verweisen. Ein Controller mit einem Typ. Daher ist es unmöglich, 3 verschiedene Typen zu analysieren. Und es macht auch keinen Sinn. – DVarga
Zu @ DVargas Kommentar hinzufügen: Es ist wirklich unklar, was Sie hier überhaupt versuchen wollen. Warum gibt es drei verschiedene Controller-Klassen? Was erwarten Sie eigentlich? Warum schreiben Sie nicht einfach den Code aus Ihrer 'initProgramm'-Methode in die' initialize() 'Methode des Controllers? –