0

I was playing around with WorkbenchFX and i cant seem to find out how to load FXML page as node.

There is an FxmlDemo (part of workbenchFX project available on link above) that loads fxml and basicaly just starts standard javafx application with fxml file.

then it goes to

public class HelloWorldModule extends WorkbenchModule {

public HelloWorldModule() {
super("Hello World", MaterialDesignIcon.HUMAN_HANDSUP);
}

 @Override
public Node activate() {
return new HelloWorldView();
}

}

and lastly this is one that i am playing with now,

public class HelloWorldView extends BorderPane {

public HelloWorldView()  {


setCenter(new Label("hi"));
// i dont want new Label here, i would like to load an fxml file instead of that, however it does not work. while just adding any other node works with no issues.

getStyleClass().add("module-background");

}

}

now instead setting a new label in the border pane, i would like to load an actual FXML file

 Parent root = FXMLLoader.load(getClass().getResource("test.fxml"));

Every time i get null pointer exception, i tryed loading this as Node, but it always fails. I did run through documentation but i am afraid i am missing something, and aside from the official documentation its impossible to find anything else.

Perhaps loading of the fxml file happens at later time so when i try to add it to border pane there is nothing to be added.

    Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at com.dlsc.workbenchfx.modules.helloworld.HelloWorldView.<init>(HelloWorldView.java:18)
    at com.dlsc.workbenchfx.modules.helloworld.HelloWorldModule.activate(HelloWorldModule.java:18)
    at com.dlsc.workbenchfx.Workbench.lambda$initListeners$0(Workbench.java:419)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
    at javafx.beans.property.ObjectProperty.setValue(ObjectProperty.java:69)
    at com.dlsc.workbenchfx.Workbench.openModule(Workbench.java:499)
    at com.dlsc.workbenchfx.view.controls.module.Tile.open(Tile.java:73)
    at com.dlsc.workbenchfx.view.controls.module.Tile.lambda$setupEventHandlers$1(Tile.java:66)
    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.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:394)
    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$345(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    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.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$203(GtkApplication.java:139)
    at java.lang.Thread.run(Thread.java:748)
   20.02.2019 21:55:29.049 [JavaFX Application Thread] TRACE com.dlsc.workbenchfx.Workbench - openModule - set active module to Hello World
20.02.2019 21:55:29.049 [JavaFX Application Thread] TRACE com.dlsc.workbenchfx.view.ContentView - Setting active view to module's view
20.02.2019 21:55:29.049 [JavaFX Application Thread] TRACE com.dlsc.workbenchfx.view.ContentView - Initially add view of module to the scene graph
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Children: child node is null: parent = StackPane@25ab85c4
    at javafx.scene.Parent$2.onProposedChange(Parent.java:435)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at com.dlsc.workbenchfx.view.ContentView.setContent(ContentView.java:76)
    at com.dlsc.workbenchfx.view.ContentPresenter.lambda$setupValueChangedListeners$1(ContentPresenter.java:88)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
    at javafx.beans.property.ObjectProperty.setValue(ObjectProperty.java:69)
    at com.dlsc.workbenchfx.Workbench.openModule(Workbench.java:499)
    at com.dlsc.workbenchfx.view.controls.module.Tile.open(Tile.java:73)
    at com.dlsc.workbenchfx.view.controls.module.Tile.lambda$setupEventHandlers$1(Tile.java:66)
    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.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:394)
    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$345(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    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.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$203(GtkApplication.java:139)
    at java.lang.Thread.run(Thread.java:748)
ImRaphael
  • 204
  • 3
  • 25
  • *"i tryed loading this as Node"* There seems to be a misunderstanding on your part. There's no information about the target type of the assignment passed to `FXMLLoader.load()`. The only thing that's done is that the compiler does some checks at compile time. At runtime using the wrong type on the left hand side of the assignment would result in a `ClassCastException`. – fabian Feb 20 '19 at 19:48
  • I dont think we understand eachother, i dont have an issue loading , i do get a resource (i can load this file and use it to set a stage with ) . How ever i cant use it to load this directly in to HelloworldView, i will update the question. – ImRaphael Feb 20 '19 at 20:52
  • The added stacktrace just confirms my assumption: It's the exact same stacktrace you get when passing `null` to the `FXMLLoader.load` method. If the same code worked in some other class, that's the result of the `getResource` method being called for a class in another package. This doesn't change the fact that the issue is the exact same as the one in the linked question. – fabian Feb 20 '19 at 21:10
  • you are right it was error on my part! after i put file in proper location it worked fine. – ImRaphael Feb 20 '19 at 21:51

0 Answers0