I have been working on a desktop application, where I need to use JavaFX. I have created various FXML files in a JavaFX project on eclipse built with Scene Builder. I have no issues loading any FXML file except for this one pesky file (FXMLImgList.fxml).
The main idea is that when a button is pressed, a new window appears. Here is my code for the event handler for that button:
@FXML
private void handleOnActionButtonImg(ActionEvent e) throws MalformedURLException, URISyntaxException
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/res/FXMLImgList.fxml").toURI().toURL());
VBox root = null;
try {
root = (VBox) loader.load(); // ERROR LOADING HERE
} catch (IOException e1) {
e1.printStackTrace();
}
ImgListController ilc = loader.getController();
ilc.init(imgUrl);
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner((Stage) vBox.getScene().getWindow());
stage.setScene(scene);
stage.setResizable(false);
stage.setTitle("Elegir imagen");
stage.show();
}
The project in the package explorer looks like so:
https://image.ibb.co/ihGMoS/Capture.png (Can't post images)
I get this exception from the try-catch block:
javafx.fxml.LoadException:
/C:/Users/Ximo/Desktop/UNI/2017-2018%20(UPV)/IPC/EclipseWorkspaceIPC/PL03_Ej2/bin/res/FXMLImgList.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at application.VentanaController.handleOnActionButtonImg(VentanaController.java:108)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
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:8413)
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.BehaviorSkinBase1ドル.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase1ドル.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.access1500ドル(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent353ドル(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null147ドル(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c
at javafx.scene.image.Image.validateUrl(Image.java:1121)
at javafx.scene.image.Image.<init>(Image.java:659)
at application.ImgListController.initialize(ImgListController.java:64)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 61 more
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at javafx.scene.image.Image.validateUrl(Image.java:1115)
... 64 more
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:8413)
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.BehaviorSkinBase1ドル.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase1ドル.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.access1500ドル(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent353ドル(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null147ドル(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException: Root cannot be null
at javafx.scene.Scene.<init>(Scene.java:336)
at javafx.scene.Scene.<init>(Scene.java:194)
at application.VentanaController.handleOnActionButtonImg(VentanaController.java:116)
... 58 more
What's even stranger, the rest of the FXML files load just fine, for example this one:
@FXML
private void handleOnActionButtonMod(ActionEvent event)
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/res/FXMLVentana.fxml"));
VBox root = null;
try {
root = (VBox) loader.load(); // NO ERROR LOADING HERE
} catch (IOException e) {
e.printStackTrace();
}
VentanaController vac = loader.getController();
vac.init(observablePersonData, table.getSelectionModel().selectedIndexProperty().getValue());
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(this.stage);
stage.setScene(scene);
stage.setResizable(false);
stage.setTitle("Modificar");
stage.show();
}
The idea behind this event handler is pretty much the same, press button -> window opens and works just fine. I've tried many this like editing the returned URL but nothing seems to work.
I've notice the message on line 67:
Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c
However the result of:
System.out.println(getClass().getResource("/res/FXMLVentana.fxml"));
System.out.println(getClass().getResource("/res/FXMLImgList.fxml"));
Outputs:
file:/C:/Users/Ximo/Desktop/UNI/2017-2018%20(UPV)/IPC/EclipseWorkspaceIPC/PL03_Ej2/bin/res/FXMLVentana.fxml
file:/C:/Users/Ximo/Desktop/UNI/2017-2018%20(UPV)/IPC/EclipseWorkspaceIPC/PL03_Ej2/bin/res/FXMLImgList.fxml
Here's the code for FXMLImgList.fxml:
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="12.0" xmlns=" http://javafx.com/javafx/8.0.151" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ImgListController">
<children>
<HBox alignment="CENTER_RIGHT" spacing="12.0">
<children>
<Button fx:id="cancelar" mnemonicParsing="false" onAction="#handleOnActionButtonCancelar" text="Cancelar" />
<Button fx:id="salvar" mnemonicParsing="false" onAction="#handleOnActionButtonSalvar" text="Salvar" />
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</HBox>
</children>
<padding>
<Insets bottom="12.0" left="12.0" right="12.0" top="12.0" />
</padding>
</VBox>
And here's FXMLVentana.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="vBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="12.0" xmlns="http://javafx.com/javafx/8.0.151" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.VentanaController">
<children>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Label prefHeight="17.0" prefWidth="53.0" text="DNI" />
<TextField fx:id="dni" />
</children>
</HBox>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Label prefHeight="17.0" prefWidth="53.0" text="Nombre" />
<TextField fx:id="nombre" />
</children>
</HBox>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Label prefHeight="17.0" prefWidth="52.0" text="Apellidos" />
<TextField fx:id="apellidos" />
</children>
</HBox>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Label prefHeight="17.0" prefWidth="52.0" text="Ciudad" />
<TextField fx:id="city" />
</children>
</HBox>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Label prefHeight="17.0" prefWidth="52.0" text="Provincia" />
<TextField fx:id="province" />
</children>
</HBox>
<HBox alignment="CENTER" spacing="12.0">
<children>
<ImageView fx:id="imgView" fitHeight="80.0" fitWidth="80.0" onMouseClicked="#handleImgMouseClick" pickOnBounds="true" preserveRatio ="true" />
<Button fx:id="buttonImg" mnemonicParsing="false" onAction="#handleOnActionButtonImg" text="Imagen" />
</children >
</HBox >
< HBox alignment="CENTER_RIGHT" spacing="12.0">
<children>
<Button fx:id="buttonCancelar" mnemonicParsing="false" onAction="#handleOnActionButtonCancelar" text="Cancelar" />
<Button fx:id="buttonAction" mnemonicParsing="false" onAction="#handleOnActionButtonAction" text="Salvar" />
</children>
</HBox>
</children>
<padding>
<Insets bottom="12.0" left="30.0" right="30.0" top="12.0" />
</padding>
</VBox>
In conclusion, I'm all out of ideas and nothing I find onlince seems to solve the issue. Any help is appreciated, thanks!
-
Show your project structure.SedJ601– SedJ6012018年03月06日 22:55:31 +00:00Commented Mar 6, 2018 at 22:55
1 Answer 1
In your stacktrace there are 2 exceptions.
The one at the end simply indicates that root is null when you try to pass it to the constructor of Scene. This happens since loading the fxml failed. This is not because of an invalid fxml url, it's the result of creating a Image with a value that is not a URL as constructor parameter in the ImgListController.initialize method. Probably you pass an absolute file path.
You could use the File or Path classes to get a URL for a file:
File f = ...
URL url = f.toURI().toURL();
Image img = new Image(url.toExternalForm());
Note: You should not simply proceed with your logic if an exception happens and the rest of the method relies on the try-block from finishing without an exception. This way you get 2 exceptions, not just one.
Alternatives would be:
Rethrowing
throw new IllegalStateException("This should not happen", e1);
- Returning after showing an error to the user/logging the error.
- ect...