JavaFX- NullPointerException从父节点中删除节点时

时间:2018-05-09 10:26:32

标签: java javafx-8 scenebuilder

我正在使用Scenebuilder和JavaFX 8。

我在ComboBox中有一个VBox,当ComboBox在一个简单条件下启动时,我试图从VBox中移除Application。这是我的尝试:

Main.java

public class Main extends Application {

    @Override
    public void start(Stage window) throws IOException {
        int QueryValue = 1;
        if (QueryValue == 1) {
            FXMLLoader loader=new FXMLLoader();

    Parent root = loader.load(getClass().getResource("/com/pos/view/login.fxml"));
    LoginController lc= (LoginController)loader.getController();
            lc.setInvisible();
            Scene scene = new Scene(root);
            window.setScene(scene);
            window.show();
        }
    }
    // ...
}

Controller类:LoginController.java

@FXML
private ComboBox<?> organization_combo;
@FXML
private  VBox vbox;
public  void setInvisible() {
    vbox.getChildren().remove(organization_combo);
}

我在FXML中添加了控制器类,并为VBoxComboBox提供了fx:id。问题是每当我致电setInvisible()时,我都会NullPointerException。但是当我没有调用setInvisible()方法时,我的fxml正在加载。谁能帮我?

N.B:我跟着JavaFX - How to delete a specific Node from an AnchorPane,我的问题与标记的问题不匹配。

这是错误消息。

Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
    at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at com.pos.view.LoginController.setInvisible(LoginController.java:45)
    at com.pos.main.Main.start(Main.java:26)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)

0 个答案:

没有答案