JavaFX中的NullPointerException在Main函数中初始化三个Controller类时

时间:2016-04-12 19:45:01

标签: exception javafx controller initializing

在Main类中初始化我的FXML时,我在JavaFX中得到NullPointerException。

我在每个Controller类中调用Main函数initProgramm()并计算调用次数。

@Override
public void initialize(URL url, ResourceBundle rb) {

    Main.initProgramm();
} 

主要课程

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);
    }
}

上次错误:

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

我认为异常的根本原因是控制器类的引用?!

    public static ControllerMain controllerMain;
    public static ControllerTabManual controllerTabManual;
    public static ControllerTabCode controllerTabCode;

1 个答案:

答案 0 :(得分:0)

Parent root = (Parent) loader.load();

这将调用您的控制器的initialize方法(ControllerMain);

在此方法中,您可以调用initProgramm。在这种方法中,您可以调用controllerMain.menuLanguageOne.setDisable(true); // LINE 71

所以在这里你要使用controllerMain。但是!

controllerMain = (ControllerMain) loader.getController();

controllerMain变量在这里获取值。但此时此行未达到,因为它位于Parent root = (Parent) loader.load();行之后。

因此,你有一个很好的NullPointerException。

但是,请阅读我对OP的评论。