0

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 VBoxand 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)
brimble2010
  • 17,796
  • 7
  • 28
  • 45
Aritra Paul
  • 834
  • 1
  • 8
  • 14
  • Injection to `static` fields does not work. Use non-`static` fields, a `FXMLLoader` instance and a non-`static` `load` method. This allows you to access the controller instance using `FXMLLoader.getController` after calling `load`. – fabian May 09 '18 at 10:35
  • Are you sure that this code compiles? Since `organization_combo` isn't static but you use it in the static method `setInvisible()` this should raise the error `Cannot make a static reference to the non-static field organization_combo` –  May 09 '18 at 10:45
  • I have edited my code as @fabian suggested but same error remains. – Aritra Paul May 09 '18 at 10:49
  • My code is running well when I stop calling setInvisible() method @devpuh – Aritra Paul May 09 '18 at 10:50
  • 1
    @devpuh You're still using a `static` `load` method. Pass the URL via constructor parameter or `setLocation` instead: `FXMLLoader loader=new FXMLLoader(getClass().getResource("/com/pos/view/login.fxml"));Parent root=loader.load(); LoginController lc=loader.getController();` – fabian May 09 '18 at 11:06
  • Still same error remains @devpuh – Aritra Paul May 09 '18 at 11:07
  • @AritraPaul see the comment from @fabian, that's the correct way to get the controller. Please add the FXML-file when you still get a `NullpointerException`. –  May 09 '18 at 11:18
  • It worked!! But this approach will also work. ` FXMLLoader loader=new FXMLLoader(getClass().getResource("/com/pos/view/login.fxml")); Parent root =loader.load(); LoginController lc= loader.getController();` – Aritra Paul May 09 '18 at 11:22

0 Answers0