2

I am newbie in JavaFX. Can anyone help me?

Here is my code

Stage stage = new Stage();

FXMLLoader loader = new FXMLLoader();
Parent root = (Parent) loader.load(getClass().getResource("FXMLMain.fxml").openStream());
FXMLMainController mainController = loader.getController();
mainController.setStage(stage);
stage.setScene(new Scene(root));
stage.show();

((Node) (event.getSource())).getScene().getWindow().hide();

loader.getController() returns null

java.lang.NullPointerException
    at library.management.system.FXMLDocumentController.loginButtonAction(FXMLDocumentController.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1763)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1651)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    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.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:49)
    at javafx.event.Event.fireEvent(Event.java:204)
    at javafx.scene.Node.fireEvent(Node.java:8175)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    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.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:204)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
    at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
    at com.sun.glass.ui.View.notifyMouse(View.java:898)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    at java.lang.Thread.run(Thread.java:744)
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
thesaba
  • 23
  • 1
  • 4
  • where you have the FXMLMain.fxml file? is it in same directory where the java class resides? – AJJ Jun 26 '14 at 12:36
  • try this too... Parent root = (Parent) loader.load(getClass().getResource("FXMLMain.fxml").toExternalForm()); – AJJ Jun 26 '14 at 12:39
  • Also take a look at this... http://stackoverflow.com/questions/23461148/fxmlloader-getcontroller-returns-null – AJJ Jun 26 '14 at 12:39
  • Most probably, your `Button` is not initialised in the controller ! – ItachiUchiha Jun 26 '14 at 13:19
  • @Jayaprasad Of course my FXMLMain.fxml is in the same directory where I have the java class. I also tried Parent root = (Parent) loader.load(getClass().getResource("FXMLMain.fxml").toExternalForm()); but it shows me an error "no suitable method found for load(String)" – thesaba Jun 26 '14 at 14:08
  • @ItachiUchiha My button is initialized in the controller – thesaba Jun 26 '14 at 14:12
  • @James_D this is line 69 mainController.setStage(stage); – thesaba Jun 26 '14 at 17:26
  • Do you have an `fx:controller` attribute set in your FXML? – James_D Jun 26 '14 at 18:47

4 Answers4

12

This not work

FXMLLoader loader = new FXMLLoader();
loader.load(url);
FXMLController controller = loader.getController(); // return null

This work

FXMLLoader loader = new FXMLLoader(url);
FXMLController controller = loader.getController(); // return the controller
Nigel
  • 121
  • 1
  • 2
  • This answer probably saved me hours of brooding and self doubt. Is this a bug, bad design, or is there actually a good reason for it? – Jannis Alexakis Nov 05 '19 at 11:49
4

add the fx:controller attribute to the root item and give the controller java file name. for example: if your root item is AnchorPane and FXMLMainController the controller java file then

<AnchorPane fx:controller:"FXMLMainController">
</AnchorPane>

the root item in the FXMLMain.fxml add fx:controller="/folder/controllerjavafilename"

Note: I assume that your fxml and controller file are the same place.

sancho
  • 598
  • 2
  • 8
  • 22
1

I know my answer is late, but I will put to benefit anyone who will fall on this page after.

short answer:

change this:

FXMLLoader loader = new FXMLLoader();
Parent root = (Parent) loader.load(getClass().getResource("FXMLMain.fxml").openStream());
FXMLMainController mainController = loader.getController();

to that:

FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLMain.fxml"));
Parent root = loader.load();
FXMLMainController mainController = loader.getController();

Explaination:

The problem is that you are invoking the load() method (which is a static method) using an instance variable instead of the FXMLLoader class. It is understandable that you need to use the instance so that you can invoke the getController() method. In this case, You should pass the URL (using getClass().getResource()) to the constructor of the FXMLLoader while you are creating your instance, then you can get the Parent root just by using the load() method. Your order of method calls is correct so just as a reminder for other, remember that you need to call the load() method first before getController() or else it will return null

I ran into this problem before and this is how I solved it.

Zeyad
  • 537
  • 2
  • 7
  • 15
-1

this works

FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource(xmlNm+".fxml"));
Parent rootLayout = loader.load();
FXMLController controller = loader.getController();
FelixSFD
  • 6,052
  • 10
  • 43
  • 117