0

I get Null Pointer Exception while I am trying to set the controller in initialize method.

Here is my Main class:

public class Main extends Application {

@Override
public void start(Stage stage) throws IOException {
    Locale.setDefault(new Locale("en"));
    ResourceBundle resourceBundle = ResourceBundle.getBundle("bundles.language");
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/mainBorderPane.fxml"),resourceBundle);
    Parent parent = fxmlLoader.load();
    Scene scene = new Scene(parent);
    stage.setScene(scene);
    stage.setTitle(resourceBundle.getString("title.app"));
    stage.show();

    ConnectionClass.initializeDB();
}

public static void main(String[] args) {
    launch();
}
}

Main Controller class:

public class MainController {

private MenuButtonsController menuButtonsController;
@FXML
private BorderPane mainBorderPane;

@FXML
private void initialize(){
    menuButtonsController.setMainController(this);
}

public void setCenter(String fxml){
    ResourceBundle resourceBundle = ResourceBundle.getBundle("bundles.language");
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxml),resourceBundle);
    Parent parent = null;
    try {
        parent = fxmlLoader.load();
    } catch (IOException ioe){
        ioe.printStackTrace();
    }
    mainBorderPane.setCenter(parent);
}
}

Menu buttons Controller:

public class MenuButtonsController {

private final String USERS_LIB_FXML = "/fxml/usersMenu.fxml";
private final String BOOKS_LIB_FXML = "/fxml/bookLib.fxml";
private final String GAMES_LIB_FXML = "/fxml/gamesLib.fxml";
private final String MOVIES_LIB_FXML = "/fxml/movieLib.fxml";
private final String OPTIONS_LIB_FXML = "/fxml/options.fxml";
@FXML
private ToggleGroup menuToggleGroup;

private MainController mainController;

@FXML
public void openUsers() {
    mainController.setCenter(USERS_LIB_FXML);
}

@FXML
public void openBookLib() {
    mainController.setCenter(BOOKS_LIB_FXML);
}

@FXML
public void openMovieLib() {
    mainController.setCenter(MOVIES_LIB_FXML);
}

@FXML
public void openGameLib() {
    mainController.setCenter(GAMES_LIB_FXML);
}

@FXML
public void openOptions() {
    mainController.setCenter(OPTIONS_LIB_FXML);
}

@FXML
public void handleExit() {
    if (menuToggleGroup.getSelectedToggle() != null) {
        menuToggleGroup.getSelectedToggle().setSelected(false);
    }
    Platform.exit();
}

public void setMainController(MainController mainController) {
    this.mainController = mainController;
}
}

Main Window FXML:

    <BorderPane fx:id="mainBorderPane" prefHeight="800.0" prefWidth="1200.0" styleClass="borderPane"
            stylesheets="@../css/mainBorderPane_stylesheet.css" xmlns="http://javafx.com/javafx/11.0.1"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.kodpalmowy.controllers.MainController">
   <left>
      <VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
          <fx:include fx:id="mainMenu" source="menuButtons.fxml" />
      </VBox>
   </left>
   <center>
      <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
          <fx:include source="welcomeScreen.fxml" />
      </HBox>
   </center>
</BorderPane>

Stack:

    Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: javafx.fxml.LoadException: 
/Users/radek/IdeaProjects/EntertainmentLib/target/classes/fxml/mainBorderPane.fxml

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
    at com.kodpalmowy/com.kodpalmowy.Main.start(Main.java:24)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2591)
    ... 9 more
Caused by: java.lang.NullPointerException
    at com.kodpalmowy/com.kodpalmowy.controllers.MainController.initialize(MainController.java:19)
    ... 20 more
Exception running application com.kodpalmowy.Main

Process finished with exit code 1

The whole code is available on my GitHub: https://github.com/kodpalmowy/EntertainmentLib

Everything was working just fine till I started working on database classes and made little refactoring. When I delete the setter from initialize() method the app starts but obviously I cannot change the scenes. I have been looking for a problem for a long time before I have decided to post here. I didn't gave up just like that so please don't hate me I am new to Java/JavaFX :)

to_getHer
  • 26
  • 4
  • 1
    Where do you initialize `private MenuButtonsController menuButtonsController;`? – Michael Kreutz May 10 '20 at 12:55
  • You need to 1. annotate `menuButtonsController` with `@FXML`. 2. Change the `fx:id` from `mainMenu` to `menuButtons`. See https://openjfx.io/javadoc/14/javafx.fxml/javafx/fxml/doc-files/introduction_to_fxml.html#nested_controllers – James_D May 10 '20 at 13:02

0 Answers0