Element "FileChooser" something is wrong with the path of input stream cannot be null

Hello
IM test 'FileChooser' element for the moment.
That's what I got:
                FileChooser fileChooser = new FileChooser();
                FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.png");
                fileChooser.getExtensionFilters().add(extFilter);

                file = fileChooser.showOpenDialog(null);

                iv.setImage(new javafx.scene.image.Image(getClass().getResourceAsStream(file.getPath())));
How to get:
java.lang.NullPointerException: Input stream must not be null
file.getAbsolutPath () does not work either, I thought that was the reason.
what I'm doing wrong here?
p.png = picture in the filechooser package.
 
iv.setImage(new javafx.scene.image.Image(getClass().getResourceAsStream(p.png)));
works very well.
Thanks for the help.
IM using windows.

The file picker chooses a file anywhere on the file system operating system.
Class resources are usually not files for JavaFX applications as javafx applications are usually packaged as jars.

When you use getClass () .getResourceAsStream (someLocation), you specify to use the protocol used by the class loader of the class, who, in a jar, packed JavaFX application will be a protocol of pot, but you do not provide a location Protocol valid at your call jar, so that the system can not find the resource you ask to find. You cannot use an element "FileChooser" choose files in a jar.

Instead, you must use something like the following using the Protocol file to access the file selected in the OS file system by the file picker:

iv.setImage(new javafx.scene.image.Image(file.toURI().toURL().toExternalForm()));

Here is an example png image viewer application where you can see it in action: https://gist.github.com/jewelsea/5165446

Tags: Java

Similar Questions

Maybe you are looking for