When using the static FXMLLoader.load(URL) method the following FXML loads correctly:
<?fxml version="1.0" encoding="utf-8"?>
<?import javafx.scene.image.*?>
<ImageView>
<image
<Image url="@images/my_image.png"/>
</image>
</ImageView>
However when using the non-static FXMLLoader.load(InputStream) class, this fails with the following error:
Caused by: java.lang.InstantiationException: javafx.scene.image.Image
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:325)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:607)
I have tried using relative and absolute paths (i.e. with and without '/' on the front) and also tried putting the image in the same package as both the loading class and the FXML file. All have the same result.
Minimal setup to reproduce (using the FXML defined above):
public class TestApp extends Application
{
public static void main(String[] args)
{
Application.launch(TestApp.class, args);
}
public void start(Stage stage) throws Exception
{
// Node node = (Node) FXMLLoader.load(getClass().getResource("/test.fxml")); this one works
FXMLLoader loader = new FXMLLoader();
Node node = (Node) loader.load(getClass().getResourceAsStream("/test.fxml")); // this one fails
VBox box = new VBox();
box.getChildren().add(node);
Scene scene = new Scene(box);
scene.getStylesheets().add("stylesheet.css");
stage.setScene(scene);
stage.setWidth(1024);
stage.setHeight(768);
stage.show();
}
}
This bug was spotted by Robert. Forum topic here: https://forums.oracle.com/forums/thread.jspa?threadID=2279877&tstart=0
<?fxml version="1.0" encoding="utf-8"?>
<?import javafx.scene.image.*?>
<ImageView>
<image
<Image url="@images/my_image.png"/>
</image>
</ImageView>
However when using the non-static FXMLLoader.load(InputStream) class, this fails with the following error:
Caused by: java.lang.InstantiationException: javafx.scene.image.Image
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:325)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:607)
I have tried using relative and absolute paths (i.e. with and without '/' on the front) and also tried putting the image in the same package as both the loading class and the FXML file. All have the same result.
Minimal setup to reproduce (using the FXML defined above):
public class TestApp extends Application
{
public static void main(String[] args)
{
Application.launch(TestApp.class, args);
}
public void start(Stage stage) throws Exception
{
// Node node = (Node) FXMLLoader.load(getClass().getResource("/test.fxml")); this one works
FXMLLoader loader = new FXMLLoader();
Node node = (Node) loader.load(getClass().getResourceAsStream("/test.fxml")); // this one fails
VBox box = new VBox();
box.getChildren().add(node);
Scene scene = new Scene(box);
scene.getStylesheets().add("stylesheet.css");
stage.setScene(scene);
stage.setWidth(1024);
stage.setHeight(768);
stage.show();
}
}
This bug was spotted by Robert. Forum topic here: https://forums.oracle.com/forums/thread.jspa?threadID=2279877&tstart=0