I am using Scenebuilder and JavaFX 8.
I have a ComboBox
in a VBox
and I am trying to remove the ComboBox
from the VBox
when the Application
starts on a simple condition. This is my attempt:
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 class:LoginController.java
@FXML
private ComboBox<?> organization_combo;
@FXML
private VBox vbox;
public void setInvisible() {
vbox.getChildren().remove(organization_combo);
}
I have added my controller class in FXML and also give fx:id for VBox
and ComboBox
. The problem is whenever I call setInvisible()
I am getting NullPointerException
. But my fxml is loading well when I am not calling that setInvisible()
method. Can anyone help me?
N.B: I have followed JavaFX - How to delete a specific Node from an AnchorPane and my problem is not matching the marked one.
Here is error message.
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)