I have a StackPane as my root, and have loaded a BorderPane layout over it in the Main Application Start() method. In the BorderPane layout I have 2 buttons: One to add an fxml file (Menu.fxml) and the other to remove this file. Once the Menu.fxml is loaded, I have a button in Menu.fxml which when clicked should load an fxml file (One.fxml) in the centre of the previously loaded BorderPane layout. However, at this stage I get a null pointer exception. Can you please point out where am I going wrong. Thank you.
Below is my code:
public class Main extends Application {
private static StackPane rootStack;
private static BorderPane rootBorder;
@Override
public void start(Stage stage) throws Exception {
rootStack = FXMLLoader.load(getClass().getResource("Base.fxml"));
rootBorder = FXMLLoader.load(getClass().getResource("Border.fxml"));
rootStack.getChildren().addAll(rootBorder);
Scene scene = new Scene(rootStack);
stage.setScene(scene);
stage.show();
} // start
public static StackPane getRootStack() {
return rootStack;
} // end of method getRootStack
public static BorderPane getRootBorder() {
return rootBorder;
} // end of method getRootBorder
public static void main(String[] args) {
launch(args);
} // launch
} // Main
public class BorderController {
private StackPane rootStack = Main.getRootStack();
VBox menu;
public void initialize() throws IOException {
menu = FXMLLoader.load(getClass().getResource("Menu.fxml"));
} // initialize
@FXML
private void addMenuPane(ActionEvent event) throws IOException {
rootStack.getChildren().add(menu);
} // addMenuPane
@FXML
private void removeNode(ActionEvent event) {
rootStack.getChildren().remove(menu);
} // removeNode
} // BorderController
public class MenuController {
private BorderPane rootBorder = Main.getRootBorder();
@FXML
private JFXButton oneButton;
public void initialize() {
oneButton.addEventHandler(MouseEvent.MOUSE_CLICKED, (event) -> {
try {
rootBorder.setCenter(FXMLLoader.load(getClass().getResource("One.fxml")));
} catch (IOException ex) {
Logger.getLogger(MenuController.class.getName()).log(Level.SEVERE, null, ex);
}
});
} // initialize
} // MenuController
Stack Trace:
Executing C:\Users\User\Documents\NetBeansProjects\Stack\dist\run2093523570\Stack.jar using platform C:\Program Files\Java\jdk1.8.0_121\jre/bin/java
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at stack.MenuController.lambda$initialize$0(MenuController.java:22)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Deleting directory C:\Users\User\Documents\NetBeansProjects\Stack\dist\run2093523570
rootBorderis null. Can you check that?rootBorderis null in the event handler inMenuController. Just put a debug line into the event handler, e.g.if (rootBorder == null) System.out.println("Warning: rootBorder is null");and see if the message is displayed. Or put a breakpoint at the beginning of the event handler and run it in your debugger.