The program basically display choice box and a button and whatever the user choose it will print it in a label in scene 2. But this error just keep coming.
There are to errors occurs one when I select an item from choice box menu and the other when I press the button.
Note: I'm using same Controller class for both fxml files.
The error:
javafx.fxml.LoadException:
/C:/Users/yasse/eclipse-workspace/FinalProject/bin/application/exscene2.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
at application.BookController.switchscene(BookController.java:57)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
at javafx.controls/javafx.scene.control.ChoiceBox$4.invalidated(ChoiceBox.java:333)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at javafx.controls/javafx.scene.control.ChoiceBox.setValue(ChoiceBox.java:342)
at javafx.controls/javafx.scene.control.ChoiceBox.lambda$new$1(ChoiceBox.java:194)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:80)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.ChoiceBox.getItems()" because "this.box" is null
at application.BookController.initialize(BookController.java:44)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655)
... 79 more
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "javafx.scene.control.Label.setText(String)" because "this.label" is null
at application.BookController.switchscene(BookController.java:66)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
at javafx.controls/javafx.scene.control.ChoiceBox$4.invalidated(ChoiceBox.java:333)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at javafx.controls/javafx.scene.control.ChoiceBox.setValue(ChoiceBox.java:342)
at javafx.controls/javafx.scene.control.ChoiceBox.lambda$new$1(ChoiceBox.java:194)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:80)
at javafx.base/javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:113)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at javafx.controls/javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:105)
at javafx.controls/javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:223)
at javafx.controls/javafx.scene.control.SingleSelectionModel.select(SingleSelectionModel.java:149)
at javafx.controls/javafx.scene.control.ChoiceBox$ChoiceBoxSelectionModel.select(ChoiceBox.java:586)
at javafx.controls/javafx.scene.control.skin.ChoiceBoxSkin.lambda$addPopupItem$7(ChoiceBoxSkin.java:390)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.controls/javafx.scene.control.MenuItem.fire(MenuItem.java:459)
at javafx.controls/com.sun.javafx.scene.control.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1385)
at javafx.controls/com.sun.javafx.scene.control.ContextMenuContent$MenuItemContainer.lambda$createChildren$12(ContextMenuContent.java:1338)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:831)
javafx.fxml.LoadException:
/C:/Users/yasse/eclipse-workspace/FinalProject/bin/application/exscene2.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
at application.BookController.switchscene(BookController.java:57)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
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:77)
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:275)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.ChoiceBox.getItems()" because "this.box" is null
at application.BookController.initialize(BookController.java:44)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655)
... 65 more
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:831)
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:78)
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:77)
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:275)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
... 46 more
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.Label.setText(String)" because "this.label" is null
at application.BookController.switchscene(BookController.java:66)
... 57 more
My Main:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
public class Main extends Application {
//#Override
public void start( Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
AnchorPane mainpane= (AnchorPane) FXMLLoader.load(Main.class.getResource("exscene1.fxml"));
primaryStage.setScene(new Scene(mainpane));
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
My Controller class:
package application;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
public class BookController implements Initializable {
#FXML
private Button button;
#FXML
private AnchorPane scene1pane;
#FXML
private ChoiceBox<String> box;
#FXML
private AnchorPane scene2pane;
#FXML
private Label label;
#FXML
private Label label1;
ObservableList<String> test = FXCollections.observableArrayList("AAA","BBB","CCC");
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
box.getItems().addAll(test);
box.setOnAction(this::switchscene);
}
public void switchscene(ActionEvent event) {
AnchorPane pane;
try {
pane = FXMLLoader.load(getClass().getResource("exscene2.fxml"));
scene1pane.getChildren().setAll(pane);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = box.getValue();
label.setText(str);
}
}
My first scene fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:id="scene1pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.BookController">
<children>
<Button fx:id="button" layoutX="223.0" layoutY="162.0" mnemonicParsing="false" onAction="#switchscene" text="Button">
<font>
<Font size="36.0" />
</font>
</Button>
<Label fx:id="label1" layoutX="139.0" layoutY="50.0" prefHeight="27.0" prefWidth="148.0" text="Scene 1">
<font>
<Font size="42.0" />
</font>
</Label>
<ChoiceBox fx:id="box" layoutX="303.0" layoutY="31.0" prefWidth="150.0" />
</children>
</AnchorPane>
My second scene fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:id="scene2pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.BookController">
<children>
<Label fx:id="label" layoutX="262.0" layoutY="92.0" text="Welcome">
<font>
<Font size="33.0" />
</font>
</Label>
</children>
</AnchorPane>
There are many things to consider in this example. Frist, let me explain the errors which your are facing.
Firstly, on click of button or choosing the choice box, you are getting constructLoadException. This is because you are using the same Controller class for both the fxmls. And you are are expecting both the fxmls to share the same Controller instance. This will not be the case. With your code, each fxml will have its own instance of BookController. So in fxml1 controller, all the instance variables related to fxml2 will be null and vice versa.
So when you initiate the fxml2, a new instance of BookController is created and the initialize method is called. As there is no "box" node in fxml2, that variable will be null and obviously will throw NPE, which is causing the exception to load the fxml.
The second error of NPE is also same, this time you are expecting the fxml2 instance variable "label" in fxml1 controller.
Solution:
There are two ways to address this issue. It depends on whether you want to share the same Controller class or If you are ok to go with separate controller class for each fxml.
#1. Using same controller class
In this way, you need to do the below two changes.
Remove the controller declaration in the exscene2 fxml.
Change the initialize and switchScene methods as below. The main changes are we
are setting the same controller instance manually to the fxml2
loader. And doing some checks in initialize method, to not re-add the
items again.
.
public void initialize(URL arg0, ResourceBundle arg1) {
if(box.getItems().isEmpty()) {
box.getItems().addAll(test);
box.setOnAction(this::switchscene);
}
}
public void switchscene(ActionEvent event) {
AnchorPane pane;
try {
// Initiate a loader for the fxml 2
final FXMLLoader loader = new FXMLLoader(getClass().getResource("exscene2.fxml"));
// Set this controller instance to fxml 2
loader.setController(this);
// And load the fxml 2.
pane = loader.load();
// By this line, both fxmls use same instance of controller
scene1pane.getChildren().setAll(pane);
} catch (IOException e) {
e.printStackTrace();
}
String str = box.getValue();
if(str!=null && !str.isEmpty()) {
label.setText(str);
}
}
#2. Using separate controller class
In this way, you need to do the below two changes.
Create a new Controller class for the fxml 2 (lets say BookController2.java) with instance variables that are only related to fxml 2.
.
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import java.net.URL;
import java.util.ResourceBundle;
public class BookController2 implements Initializable {
#FXML
private AnchorPane scene2pane;
#FXML
private Label label;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
// Empty
}
public final Label getLabel(){
return label;
}
}
Set this BookController2 as controller in fxml 2.
fx:controller="application.BookController2"
In BookController.java remove all the instance variables of fxml 2 and change the switchScene method as below.
.
public void switchscene(ActionEvent event) {
AnchorPane pane;
try {
// Initiate the loader for fxml 2. Using this loader, you can get the controller instance of fxml 2
final FXMLLoader loader = new FXMLLoader(getClass().getResource("exscene2.fxml"));
pane = loader.load();
scene1pane.getChildren().setAll(pane);
String str = box.getValue();
if (str != null && !str.isEmpty()) {
// Get the label from the controller and set the text.
((BookController2) loader.getController()).getLabel().setText(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
Related
I am working on image editor using JavaFX FXML ,scene builder 2.0 .I am getting this error continuously where my scene builder doesn't read FXML file when double clicking on it. I haven't used an other other libraries or anything and have tried opening particular FXML from scene builder directly as well.
I have tried using possible solutions on this question but non seems to work for me.
enter image description here
package imageeditor;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javax.imageio.ImageIO;
import imageeditor.CommandCenter;
import javafx.scene.control.ColorPicker;
public class FXMLDocumentController implements Initializable {
// #FXML
// private Edit Images = new Edit();
// #FXML
// private ImageView ImageView,i ;
// #FXML private ImageView mImageView;
#FXML
private AnchorPane mAnchorPane;
#FXML
private ImageView mImageView;
#FXML
private Button btnUndo;
#FXML
private Button btnRedo;
#FXML
private Button exit;
#FXML
private Button reset;
#FXML
private Button uploadbtn,savebtn;
#FXML
private Label l1,l2,l3,l4,l5,l6,l7;
#FXML
private Slider bright;
#FXML
private Slider gauss;
#FXML
private Slider hue;
#FXML
private Slider saturation;
#FXML
private Slider contrast;
#FXML
private Button cropbtn ;
#FXML
private Button stickersbtn ;
#FXML
private Button textbtn ;
// #FXML
// private Button ;
#FXML
private ColorPicker framecolour ;
private ColorAdjust AdjustEffect = new ColorAdjust();
// CommandCenter CommandCenter= new CommandCenter();
#FXML
private void LoadImage(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
File file = fileChooser.showOpenDialog(null);
try
{
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
mImageView.setImage(image);
Image currentImage = getSnapshot();
CommandCenter.getInstance().setImageAndView(currentImage);
CommandCenter.getInstance().setOriginalImage(currentImage);
// CommandCenter.getInstance().setImageView(mImageView);
// CommandCenter.getInstance().setOriginalImage(image);
} catch (IOException ex) {
}
}
#FXML
private void SaveImage(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save Image");
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
File file = fileChooser.showSaveDialog(null);
if (file != null) {
try {
BufferedImage bImage = SwingFXUtils.fromFXImage(mImageView.snapshot(null, null), null);
ImageIO.write(bImage,"png", file);
} catch (IOException ex) {
}
}
}
//#FXML void quit(ActionEvent event) {
// System.exit(0);
// }
#FXML void undo(ActionEvent event) {
undo();
}
#FXML void redo(ActionEvent event) {
redo();
}
// save image as snapshot
private Image getSnapshot() {
SnapshotParameters snapshotParameters = new SnapshotParameters();
snapshotParameters.setViewport(mImageView.getViewport());
// snapshotParameters.setViewport(new Rectangle2D(mImageView.getX(), mImageView.getY(), mImageView.getImage().getWidth(), mImageView.getImage().getHeight()));
return mAnchorPane.snapshot(snapshotParameters, null);
}
private void undo() {
if (CommandCenter.getInstance().hasUndoImage()) {
Image currentImage = getSnapshot();
CommandCenter.getInstance().addRedoImage(currentImage);
Image undoImage = CommandCenter.getInstance().getUndoImage();
resetEffectsSliders();
CommandCenter.getInstance().setImageAndView(undoImage);
mImageView.setImage(undoImage);
enableRedo();
if (!CommandCenter.getInstance().hasUndoImage()) {
disableUndo();
}
}
}
// redo action
private void redo() {
System.out.println("redo image added");
if (CommandCenter.getInstance().hasRedoImage()) {
Image currentImage = getSnapshot();
CommandCenter.getInstance().addUndoImage(currentImage);
Image redoImage = CommandCenter.getInstance().getRedoImage();
resetEffectsSliders();
CommandCenter.getInstance().setImageAndView(redoImage);
mImageView.setImage(redoImage);
enableUndo();
if (!CommandCenter.getInstance().hasRedoImage()) {
disableRedo();
}
}
}
// update the image and associated properties
private void updateImageAndProperties() {
CommandCenter.getInstance().storeLastImageAsUndo();
CommandCenter.getInstance().clearRedoImages(); // new "path" so clear redo images
disableRedo();
enableUndo();
Image currentImage = getSnapshot();
CommandCenter.getInstance().setImageAndView(currentImage);
resetEffectsSliders();
mImageView.setImage(currentImage);
}
// start over with original image
private void startOver() {
resetEffectsSliders();
Image originalImage = CommandCenter.getInstance().getOriginalImage();
CommandCenter.getInstance().setImageAndView(originalImage);
mImageView.setImage(originalImage);
CommandCenter.getInstance().clearRedoImages();
CommandCenter.getInstance().clearUndoImages();
disableUndo();
disableRedo();
}
private void resetEffectsSliders() {
bright.setValue(0);
contrast.setValue(0.0);
hue.setValue(0.0);
saturation.setValue(0.0);
}
// enable undo buttons
private void enableUndo() {
btnUndo.setDisable(false);
}
// disable undo buttons
private void disableUndo() {
btnUndo.setDisable(true);
}
// enable redo buttons
private void enableRedo() {
btnRedo.setDisable(false);
}
// disable redo buttons
private void disableRedo() {
btnRedo.setDisable(true);
}
// enable start over buttons
private void enableStartOver() {
reset.setDisable(false);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
CommandCenter.getInstance().setImageView(mImageView);
// Images.GaussSliderEvent(gauss);
// Images.BrightSliderEvent(bright);
// Images.SaturationSliderEvent(saturation);
// Images.HueSliderEvent(hue);
// Images.ContrastSliderEvent(contrast);
mImageView.setEffect(AdjustEffect);
// take a snapshot to set as initial image
Image initialImage = getSnapshot();
mImageView.setImage(initialImage);
CommandCenter.getInstance().setImageView(mImageView);
CommandCenter.getInstance().setOriginalImage(initialImage);
CommandCenter.getInstance().setImageAndView(initialImage);
// brightness slider
bright.valueProperty().addListener((observable, oldValue, newValue) -> {
AdjustEffect.setBrightness(newValue.doubleValue());
mImageView.setEffect(AdjustEffect);
updateImageAndProperties();
});
// hue slider
hue.valueProperty().addListener((observable, oldValue, newValue) -> {
AdjustEffect.setHue(newValue.doubleValue());
mImageView.setEffect(AdjustEffect);
updateImageAndProperties();
});
// saturation slider
saturation.valueProperty().addListener((observable, oldValue, newValue) -> {
AdjustEffect.setSaturation(newValue.doubleValue());
//Image.setEffect(AdjustEffect);
updateImageAndProperties();
});
// contrast slider
contrast.valueProperty().addListener((observable, oldValue, newValue) -> {
AdjustEffect.setContrast(newValue.doubleValue());
// Image.setEffect(AdjustEffect);
updateImageAndProperties();
});
// gauss slider
gauss.valueProperty().addListener((observable, oldValue, newValue) -> {
// gauss.setRadius(newValue.doubleValue()*100);
// Image.setEffect(gauss);
updateImageAndProperties();
});
}
}
Java FXML file is
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="650.0" prefWidth="1150.0" style="-fx-background-color: #FFFFF0;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="imageeditor.FXMLDocumentController">
<children>
<Button fx:id="uploadbtn" layoutX="797.0" layoutY="24.0" onAction="#LoadImage" opacity="0.75" style="-fx-background-color: #800000;" text="UPLOAD IMAGE" textFill="WHITE">
<font>
<Font name="Bell MT Bold" size="18.0" />
</font></Button>
<ImageView fx:id="ImageView" fitHeight="639.0" fitWidth="927.0" layoutX="201.0" layoutY="94.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#BG.jpg" />
</image></ImageView>
<Button fx:id="savebtn" layoutX="1007.0" layoutY="23.0" mnemonicParsing="false" onAction="#SaveImage" opacity="0.75" prefHeight="34.0" prefWidth="105.0" style="-fx-background-color: #800000;" text="SAVE" textFill="WHITE">
<font>
<Font name="Bell MT Bold" size="18.0" />
</font></Button>
<Button fx:id="resetbtn" layoutX="54.0" layoutY="576.0" text="RESET" />
<Button fx:id="undobtn" layoutX="25.0" layoutY="94.0" mnemonicParsing="false" style="-fx-background-color: #800000;" text="UNDO" textFill="WHITE" />
<Button fx:id="redobtn" layoutX="115.0" layoutY="94.0" mnemonicParsing="false" style="-fx-background-color: #800000;" textFill="WHITE"text="REDO" />
<Button fx:id="cropbtn" layoutX="63.0" layoutY="143.0" mnemonicParsing="false" text="CROP" />
<ColorPicker fx:id="framecolour" layoutX="52.0" layoutY="414.0" />
<Button fx:id="stickersbtn" layoutX="24.0" layoutY="516.0" mnemonicParsing="false" text="ADD STICKERS" />
<Button fx:id="textbtn" layoutX="44.0" layoutY="470.0" mnemonicParsing="false" text="ADD TEXT" />
<Slider fx:id="gauss" blockIncrement="0.1" layoutX="42.0" layoutY="315.0" max="1.0" min="0" />
<Slider fx:id="contrast" blockIncrement="0.01" layoutX="42.0" layoutY="237.0" max="1.0" min="-1.0" />
<Slider fx:id="hue" blockIncrement="0.01" layoutX="42.0" layoutY="199.0" max="1.0" min="-1.0" />
<Slider fx:id="saturation" blockIncrement="0.01" layoutX="42.0" layoutY="277.0" max="1.0" min="-1.0" />
<Slider fx:id="bright" blockIncrement="0.01" layoutX="38.0" layoutY="355.0" max="1.0" min="-1.0" />
<Slider fx:id="frame" blockIncrement="0.01" layoutX="42.0" layoutY="393.0" max="1.0" min="-1.0" />
<ImageView fitHeight="63.0" fitWidth="91.0" layoutX="31.0" layoutY="13.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#logo.png" />
</image>
</ImageView>
<Label fx:id="l1" layoutX="140.0" layoutY="18.0" prefHeight="51.0" prefWidth="451.0" text="IMAGE EDITOR AND ENHANCER" textFill="#830c0c">
<font>
<Font name="Artifakt Element Black Italic" size="29.0" />
</font>
</Label>
<Label fx:id="l2" layoutX="81.0" layoutY="182.0" text="hue" />
<Label fx:id="l3" layoutX="76.0" layoutY="220.0" text="contrast" />
<Label fx:id="l4" layoutX="68.0" layoutY="258.0" text="saturation" />
<Label fx:id="l5" layoutX="76.0" layoutY="298.0" text="gauss" />
<Label fx:id="l6" layoutX="74.0" layoutY="331.0" text="brightness" />
<Label fx:id="l7" layoutX="74.0" layoutY="376.0" text="frame" />
</children>
</AnchorPane>
SceneBuilder bring a stacktrace if you press show details button
Details :
java.io.IOException: org.xml.sax.SAXParseException; lineNumber: 28; columnNumber: 149; Element type "Button" must be followed by either attribute specifications, ">" or "/>".
at com.oracle.javafx.scenebuilder.kit.fxom.glue.GlueLoader.load(GlueLoader.java:93)
at com.oracle.javafx.scenebuilder.kit.fxom.glue.GlueLoader.load(GlueLoader.java:76)
at com.oracle.javafx.scenebuilder.kit.fxom.glue.GlueDocument.<init>(GlueDocument.java:54)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument.<init>(FXOMDocument.java:84)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument.<init>(FXOMDocument.java:108)
at com.oracle.javafx.scenebuilder.kit.editor.EditorController.updateFxomDocument(EditorController.java:2560)
at com.oracle.javafx.scenebuilder.kit.editor.EditorController.setFxmlTextAndLocation(EditorController.java:763)
at com.oracle.javafx.scenebuilder.app.DocumentWindowController.loadFromFile(DocumentWindowController.java:389)
at com.oracle.javafx.scenebuilder.app.SceneBuilderApp.performOpenFiles(SceneBuilderApp.java:668)
at com.oracle.javafx.scenebuilder.app.SceneBuilderApp$1.invalidated(SceneBuilderApp.java:520)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(Unknown Source)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown Source)
at javafx.base/javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(Unknown Source)
at javafx.base/javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(Unknown Source)
at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(Unknown Source)
at javafx.base/javafx.beans.property.BooleanPropertyBase.set(Unknown Source)
at com.oracle.javafx.scenebuilder.kit.library.user.UserLibrary.lambda$updateFirstExplorationCompleted$7(UserLibrary.java:371)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: org.xml.sax.SAXParseException; lineNumber: 28; columnNumber: 149; Element type "Button" must be followed by either attribute specifications, ">" or "/>".
at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.seekCloseOfStartTag(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.oracle.javafx.scenebuilder.kit.fxom.glue.GlueLoader.load(GlueLoader.java:91)
... 23 more
java.io.IOException: org.xml.sax.SAXParseException; lineNumber: 28; columnNumber: 149;
Line 28:
<Button fx:id="redobtn" layoutX="115.0" layoutY="94.0" mnemonicParsing="false" style="-fx-background-color: #800000;" textFill="WHITE"text="REDO" />
Error: there is no space in between textFill and text attributes , so Compiler can't read properly button tag
fix line 28 :
<Button fx:id="redobtn" layoutX="115.0" layoutY="94.0" mnemonicParsing="false" style="-fx-background-color: #800000;" textFill="WHITE" text="REDO" />
Now scenebuilder can open that file
[enter image description here][1]Here is the Driver , The program will run with no errors but the gui will not show up at all. I am a noob to FX and i am just trying to get it to work.
public class Vacation extends Application {
private Stage stage;
#Override
public void start(Stage stage) throws Exception {
this.stage = stage;
try{
FXMLLoader loader = new
FXMLLoader(getClass().getResource("Vacation.fxml"));
AnchorPane pane = loader.load();
MainControler mainController = loader.getController();
Scene scene = new Scene(pane, 640, 450);
stage.setScene(scene);
stage.show();
}catch(IOException e){
}
}
public static void main(String[] args) {
launch(args);
}
}
Here is the FXML.
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="Application.MainControler">
<children>
<AnchorPane layoutX="9.0" layoutY="8.0" prefHeight="131.0" prefWidth="572.0" style="-fx-background-color: #333333;">
<children>
<ImageView fitHeight="106.0" fitWidth="470.0" layoutX="51.0" layoutY="13.0">
<image>
<Image url="#../../../../../Desktop/FxPics/dc3.jpg" />
</image>
</ImageView>
<Button fx:id="DC" layoutX="229.0" layoutY="53.0" mnemonicParsing="false" onAction="#DCbuttonPpess" text="Devils CourtHouse" textFill="#ee0707" />
</children>
</AnchorPane>
<AnchorPane layoutX="9.0" layoutY="149.0" prefHeight="131.0" prefWidth="572.0" style="-fx-background-color: #333333;">
<children>
<ImageView fitHeight="100.0" fitWidth="476.0" layoutX="49.0" layoutY="17.0">
<image>
<Image url="#../../../../../Desktop/FxPics/reef.jpg" />
</image>
</ImageView>
<Button fx:id="STB" layoutX="226.0" layoutY="55.0" mnemonicParsing="false" onAction="#STBbuttonPress" text="Scuba The Bahamas" textFill="#01d3f8" />
</children>
</AnchorPane>
<AnchorPane layoutX="9.0" layoutY="295.0" prefHeight="131.0" prefWidth="572.0" style="-fx-background-color: #333333;">
<children>
<ImageView fitHeight="100.0" fitWidth="488.0" layoutX="42.0" layoutY="16.0">
<image>
<Image url="#../../../../../Desktop/FxPics/SD2.jpg" />
</image>
</ImageView>
<Button fx:id="SDC" layoutX="230.0" layoutY="53.0" mnemonicParsing="false" onAction="#SDCButtonPress" text="Sky Dive Colorado" textFill="#ffa616" />
</children>
</AnchorPane>
<AnchorPane layoutX="9.0" layoutY="445.0" prefHeight="131.0" prefWidth="572.0" style="-fx-background-color: #333333;">
<children>
<ImageView fitHeight="106.0" fitWidth="488.0" layoutX="42.0" layoutY="13.0">
<image>
<Image url="#../../../../../Desktop/FxPics/c3.jpg" />
</image>
</ImageView>
<Button fx:id="BC" layoutX="248.0" layoutY="54.0" mnemonicParsing="false" onAction="#BCButtonpress" text="Baron Cliffs" textFill="#543801" />
</children>
</AnchorPane>
and here is the Controller.
public class MainControler implements Initializable {
#FXML
private Button DC;
#FXML
private Button STB;
#FXML
private Button SDC;
#FXML
private Button BC;
#FXML public void BCButtonpress(ActionEvent event) {
try{
FXMLLoader bcLoader = new
FXMLLoader(getClass().getResource("BaronCliff.fxml"));
Parent root1 = (Parent) bcLoader.load();
Stage bc = new Stage();
bc.setTitle("Baron Cliff");
bc.setScene(new Scene(root1));
bc.show();
}catch (IOException e){
System.out.println("Cant Load new Window.");
}
}
#FXML public void DCbuttonpress(ActionEvent event) {
try{
FXMLLoader bcLoader = new
FXMLLoader(getClass().getResource("DevilsCourthouse.fxml"));
Parent root1 = (Parent) bcLoader.load();
Stage bc = new Stage();
bc.setTitle("Devils Courthouse");
bc.setScene(new Scene(root1));
bc.show();
}catch (IOException e){
System.out.println("Cant Load new Window.");
}
}
#FXML public void STBbuttonPress(ActionEvent event) {
try{
FXMLLoader bcLoader = new
FXMLLoader(getClass().getResource("Scuba.fxml"));
Parent root1 = (Parent) bcLoader.load();
Stage bc = new Stage();
bc.setTitle("Scuba the Bahamas");
bc.setScene(new Scene(root1));
bc.show();
}catch (Exception e){
System.out.println("Cant Load new Window.");
}
}
#FXML public void SDC(ActionEvent event) {
try{
FXMLLoader bcLoader = new
FXMLLoader(getClass().getResource("Skydive.fxml"));
Parent root1 = (Parent) bcLoader.load();
Stage bc = new Stage();
bc.setTitle("Skydive Colorado");
bc.setScene(new Scene(root1));
bc.show();
}catch (Exception e){
System.out.println("Cant Load new Window.");
}
}
Thanks in advance for any help, I finally broke down and decided to post this, i could not find anyone with my problem probably something minor but i have no one with fresh eyes to really give it a look. apologies.
After putting in the e.printStackTrace(); in the catch block I got a list of exceptions.
javafx.fxml.LoadException: Error resolving
onAction='#DCbuttonPpess', either the event handler is
not in the Namespace or there is an error in the script.
file:/C:/Users/Kalamar/Documents/NetBeansProjects/Vacation/di
st/run201542084/Vacation.jar!/Application/Vacation.fxml:17
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at Application.Vacation.start(Vacation.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
here is the typo fixed.
<Button fx:id="DC" layoutX="229.0" layoutY="53.0" mnemonicParsing="false"
onAction="#DCbuttonpress" text="Devils CourtHouse" textFill="#ee0707"
/>enter
here is the exception after running after the fix.
Executing C:\Users\Kalamar\Documen
ts\NetBeansProjects\Vacation\dist \run1238622098\Vacation
.jar using platform C:\Program Files\Java\jdk1.8.0_151\jre/bin/java
javafx.fxml.LoadException: Error resolving onAction='#DCbuttonpress',
either the event handler is not in the Namespace or there is an
error in the script
file:/C:/Users/Kalamar/Documents/NetBeansProjects/Vacation/d
ist/run1238622098/V acation.jar!/Application/Vacation.fxml:17
The stack trace:
javafx.fxml.LoadException: Error resolving
onAction='#DCbuttonPpess', either the event handler is
not in the Namespace or there is an error in the script.
file:/C:/Users/Kalamar/Documents/NetBeansProjects/Vacation/di
st/run201542084/Vacation.jar!/Application/Vacation.fxml:17
Indicates that there is a problem with the onAction handler specified at line 17 of the Vacation.fxml file. That line is:
<Button fx:id="DC" ... onAction="#DCbuttonPpess" ... />
There is a clear typo in the onAction attribute: the method defined in the controller class is
#FXML public void DCbuttonpress(ActionEvent event) {
/* ... */
}
So the FXML should be
<Button fx:id="DC" ... onAction="#DCbuttonpress" ... />
I'm trying to load different urls into a javafx webview after a given time. I have searched for a solution for at least 2 weeks but i wasn't able to find anything or wasn't able to transfer the solutions of similar problems to my code. Below are the necessary parts of the code:
Main.java
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/FXML.fxml"));
primaryStage.setTitle("Window");
primaryStage.setScene(new Scene (root));
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
FXMLController.java
package application;
import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
public class FXMLController implements Initializable {
#FXML private Button btnAddUrl;
#FXML private Button btnStart;
#FXML private Slider sliderTime2NextUrl;
#FXML private WebView webviewWindow;
private WebEngine engine;
private FileChooser fileChooser;
private List<String> url;
private int urlTime;
public void initialize(URL arg0, ResourceBundle arg1) {
btnAddUrl.setOnAction(this::addUrls);
btnStart.setOnAction(this::urlLoad);
fileChooser = new FileChooser();
engine = webviewWindow.getEngine();
getsliderTime2NextUrl();
}//initialize
#FXML
private void addUrls(ActionEvent event) {
fileChooser.setTitle("Add Like List");
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("TXT Files", "*.TXT"),
new ExtensionFilter("txt Files", "*.txt"));
File file = fileChooser.showOpenDialog(null);
try {
url = Files.readAllLines(file.toPath());
} catch (Exception e) {
System.out.println("shit");
}
System.out.println(url);
}
#FXML
private void getsliderTime2NextUrl() {
sliderTime2NextUrl.valueProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println("Slider Value Changed (newValue: " + newValue.intValue() + ")");
double getTime;
getTime = sliderTime2NextUrl.valueProperty().getValue();
urlTime = (int) getTime;
}//changed
});//addListener
}//getsliderTime2NextUrl
#FXML
private void urlLoad (ActionEvent event) {
Task<Void> task = new Task<Void>() {
#Override protected Void call() throws Exception{
urlTime = urlTime * 1000 ;
System.out.println("next Url in: " + urlTime);
for(int i = 0; i < url.size(); i++) {
System.out.println("Url.size = "+ url.size());
System.out.println("Url = "+ i);
if(isCancelled()) {
updateMessage("Cancelled");
break;
}
updateMessage("Iteration " + i);
updateProgress(i, 1000);
try {
Thread.sleep(urlTime);
System.out.println("Thread Sleeps for :" + urlTime);
} catch (InterruptedException interrupted) {
if (isCancelled()) {
updateMessage("Cancelled");
break;
}
}
}
return null;
}
};
new Thread(task).start();
}
/*
#FXML
private void urlLoad (ActionEvent event) {
urlTime = urlTime * 1000 ;
System.out.println("next Url in: " + urlTime);
for(int i = 0; i < url.size(); i++) {
System.out.println("Url.size = "+ url.size());
System.out.println("Url = "+ i);
String Link = url.get(i);
engine.load(Link);
System.out.println("Url loaded = " + Link);
}
}*/
}
FXML.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.web.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="1.7976931348623157E308"
maxWidth="1.7976931348623157E308"
minHeight="400.0" minWidth="500.0"
prefHeight="500.0" prefWidth="500.0"
xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.FXMLController">
<children>
<MenuBar maxHeight="25.0" maxWidth="1920.0" prefHeight="25.0" prefWidth="500.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<Button fx:id="btnAddUrl" layoutX="66.0" layoutY="39.0" mnemonicParsing="false" onAction="#addUrls" prefHeight="25.0" prefWidth="69.0" text="AddUrl" />
<Button fx:id="btnStart" layoutX="36.0" layoutY="108.0" mnemonicParsing="false" onAction="#urlLoad" prefHeight="31.0" prefWidth="108.0" text="Start!">
<font>
<Font size="18.0" />
</font>
</Button>
<Slider fx:id="sliderTime2NextUrl" blockIncrement="1.0" layoutX="204.0" layoutY="39.0" majorTickUnit="2.0" max="15.0" min="3.0" minorTickCount="1" prefHeight="38.0" prefWidth="251.0" showTickLabels="true" showTickMarks="true" snapToTicks="true" value="3.0" />
<WebView fx:id="webviewWindow" layoutY="147.0" minHeight="-1.0" minWidth="-1.0" prefHeight="350.0" prefWidth="500.0" />
</children>
</AnchorPane>
I used the second "urlLoad"-Method (FXMLController.java) in the /* */ first but recognized that all Links are loaded immediately and only the last one stays active. After that I added "Thread.sleep(urlTime) + try & catch" but it freezed my whole GUI until the last URL was loaded. While searching for a solution, that was the first time I heard of workers, tasks & services.
I found out why this happened and tried to add a "Task" around the code. It was a long jorney until I found the code of the first "urlLoad"-Method in FXMLController.java. Thats exactly what my code with the Urls shoud do
![urlLoad-Method 1] there should be an image if i had 10 reputation
it gives out the numbers in the console with the given time from the slider.
Then i added my engine.load() part to the first "urlLoad"-Method
//updateMessage("Iteration " + i);
//updateProgress(i, 1000);
String Link = url.get(i);
engine.load(Link);
System.out.println("Url loaded = " + Link);
But then the method stops at the first Url and doesnt show anything:
![urlLoad-Method 1 with engine.load] there should be an image if i had 10 reputation
My assumption is, that the "urlLoad"-Method does not update the WebView Window in my GUI. I hope somebody can help me.
Thanks
Edit: After adding
task.setOnFailed((WorkerStateEvent t) -> {
throw new RuntimeException(task.getException());
});
I got the following exception:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-5
at application.FXMLController.lambda$2(FXMLController.java:111)
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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
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.concurrent.EventHelper.fireEvent(EventHelper.java:219)
at javafx.concurrent.Task.fireEvent(Task.java:1356)
at javafx.concurrent.Task.setState(Task.java:707)
at javafx.concurrent.Task$TaskCallable.lambda$call$502(Task.java:1453)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-5
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:279)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
at javafx.scene.web.WebEngine.checkThread(WebEngine.java:1243)
at javafx.scene.web.WebEngine.load(WebEngine.java:913)
at application.FXMLController$2.call(FXMLController.java:92)
at application.FXMLController$2.call(FXMLController.java:1)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
at java.util.concurrent.FutureTask.run(Unknown Source)
... 1 more
you need to use
Platform.Runlater(()->{/*your task content here*/});
Javafx is pissy about what operations it allows you to run in which thread. Essentially, you have no business running code designated for ui control functionality on a non-ui thread... hence "thread 5" in your case.
Be careful, however, as you still need that thread.sleep routine in your background thread, as the javafx thread will sleep, and then proceed with the url.load() routine.
so...
javafxapplication thread (runs - handles its own operations)
thread 5 (runs whatever you want)
->
javafxapplication thread (runs but is idle from your POV)
thread 5 (invokes platform.runlater(/* engine.load("x") */))
->
javafxapplicationthread (executes engine.load())
thread 5 (etc etc etc)
good luck
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I'm trying to click a button in my GUI and switch to a separate scene within the same stage. This seems simple except that I get a NullPointerException in my Controller class when I click the button. The button is written in the FXML with fx:id="settings" and onAction="#handleSettings". The handleSettings method is located in the controller.
I'm trying to import the primaryStage from Main.java into Controller.java by writing public Stage primaryStage; at the top, but I'm clearly missing something. The GUI launches fine but the error occurs when clicking Settings button. The full code is below along with the error. The error is pointing to this line: primaryStage.setScene(sceneSettings);
Do I need to use get/set for primaryStage somehow? Clearly I'm at level 1 in skills. Anything helps, thanks.
Main.java
import ...
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(root, 450, 580);
scene.getStylesheets().add("styleMain.css");
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setTitle("Scene 1");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Controller.java
import ...
public class Controller{
Scene sceneSettings;
public Stage primaryStage;
public void handleFieldData(){
System.out.println("field data");
}
public void handleComments(){
System.out.println("comments");
}
#FXML
public void handleSettings() throws IOException {
Button button2 = new Button("settings");
StackPane layout = new StackPane();
layout.getChildren().add(button2);
sceneSettings = new Scene(layout, 450, 580);
primaryStage.setScene(sceneSettings);
primaryStage.setTitle("Settings Menu");
primaryStage.show();
}
}
sample.fxml
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="Controller">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="20.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="20.0" vgrow="SOMETIMES" />
</rowConstraints>
<padding>
<Insets bottom="10.0" left="70.0" right="70.0" top="10.0" />
</padding>
<children>
<Button fx:id="comments" mnemonicParsing="false" onAction="#handleComments" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<padding>
<Insets bottom="15.0" left="50.0" right="50.0" top="90.0" />
</padding>
</Button>
<Button fx:id="settings" mnemonicParsing="false" onAction="#handleSettings" text="Settings" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER">
<padding>
<Insets bottom="15.0" left="30.0" right="30.0" top="15.0" />
</padding>
</Button>
<Label text="Comments" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="TOP" />
</children>
</GridPane>
Error:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
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.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$MouseHandler.process(Scene.java:3757)
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:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
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$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
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:497)
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:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 48 more
Caused by: java.lang.NullPointerException
at Controller.handleSettings(Controller.java:38)
... 58 more
When you load your controller you will need to pass it a reference to the primary stage.
public class Controller{
Scene sceneSettings;
public Stage primaryStage;
...
public void setPrimaryStage(Stage stage) {
this.primaryStage = stage;
}
...
Main
#Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("sample.fxml"));
Parent root = loader.load();
loader.getController().setPrimaryStage(primaryStage);
Scene scene = new Scene(root, 450, 580);
scene.getStylesheets().add("styleMain.css");
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setTitle("Scene 1");
primaryStage.setScene(scene);
primaryStage.show();
...
i am facing problem in navigating to the new screen on clicking the image(imageview). i have already written a code for switching between the screens using ActionEvent. now i want to navigate the screens on clicking the image. i tried doing the same but its giving type mismatch error.
my fxml looks like
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="299.0" prefWidth="439.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="multiplescreen.HomeScreenController">
<children>
<Button fx:id="button" layoutX="40.0" layoutY="245.0" onAction="#goToScreen1" text="Logout" />
<Button layoutX="321.0" layoutY="245.0" mnemonicParsing="false" onAction="#Exit" text="Exit" />
<Label layoutX="170.0" layoutY="30.0" text="Home Screen" />
<ImageView id="project" fx:id="project" fitHeight="62.0" fitWidth="85.0" layoutX="31.0" layoutY="105.0" onMouseClicked="#goToProjectCreationScreen" pickOnBounds="true" preserveRatio="true">
<!--<image>
<Image url="file:/C:/Users/user1/Documents/NetBeansProjects/Application1/Images/Project.png" />
</image>-->
</ImageView>
<ImageView fitHeight="54.0" fitWidth="75.0" layoutX="127.0" layoutY="109.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="file:/C:/Users/user1/Documents/NetBeansProjects/Application1/Images/Execute.png" />
</image>
</ImageView>
<ImageView fitHeight="62.0" fitWidth="76.0" layoutX="212.0" layoutY="105.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="file:/C:/Users/user1/Documents/NetBeansProjects/Application1/Images/AccountInfo.jpg" />
</image>
</ImageView>
<ImageView fitHeight="62.0" fitWidth="84.0" layoutX="307.0" layoutY="103.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="file:/C:/Users/user1/Documents/NetBeansProjects/Application1/Images/help.png" />
</image>
</ImageView>
<Label layoutX="49.0" layoutY="173.0" text="Projects" />
<Label layoutX="141.0" layoutY="173.0" text="Execute" />
<Label layoutX="230.0" layoutY="173.0" text="Account Info" />
<Label layoutX="336.0" layoutY="173.0" text="Help" />
</children>
</AnchorPane>
and my controller looks like
public class HomeScreenController implements Initializable,ControlledScreen,EventHandler {
ScreensController myController;
#FXML
private Label label;
#FXML
private ImageView project;
#FXML
private void goToScreen1(ActionEvent event) {
myController.setScreen(MultipleScreen.screen1ID);
}
#FXML
public void goToProjectCreationScreen(ActionEvent event) {
Image image=new Image(getClass().getResourceAsStream("Project.png"));
project.setImage(image);
project.addEventFilter(javafx.scene.input.MouseEvent.MOUSE_CLICKED, new EventHandler<javafx.scene.input.MouseEvent>() {
#Override
public void handle(javafx.scene.input.MouseEvent event) {
//To change body of generated methods, choose Tools | Templates.
System.out.println("clicked");
myController.setScreen(MultipleScreen.screen5ID);
event.consume();
}
});
//myController.setScreen(MultipleScreen.screen5ID);
}
#FXML
private void Exit(ActionEvent event) {
Platform.exit();
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#Override
public void setScreenParent(ScreensController screenParent) {
myController=screenParent;
}
#Override
public void handle(Event event) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
After excecuting I am getting the exception on clicking the image :
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
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:497)
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:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1765)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
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.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:3471)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3399)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3767)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$151/20733739.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/17230114.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
The error says it all. The method goToProjectCreationScreen() is called on onMouseClicked event, which expects a MouseEvent.
So the method should accept MouseEvent instead of ActionEvent