0

I'm creating a JavaFX application that needs to show an image, my currently way to do that is this:

        ImageView icon=new ImageView(new Image("file:///C:/Users/MMJon/eclipse-workspace/Pumaguia/src/vista/resources/rutas.png"));

i've tried to do it like this

        ImageView icon=new ImageView(new Image(this.getClass().getResource("resources/rutas.png").toExternalForm()));

but it throws a Null Pointer exception. How can i do what i need?

Here's an image of the console and here's an image of my project's structure My main class is "pumaguia" and the class that shows the image is in "pestanas" package, and the folder that contains the image is "resources".

UPDATE: I've tried to compile on ubuntu's command line and this was what i get. Hope it helps.

  • i've already tried this `vista/resources/rutas.png` and `src/vista/resources/rutas.png` as well but both haven't work – jonathanmarq Dec 03 '19 at 07:02
  • `new Image(getClass().getResourceAsStream("/vista/rutas.png"));` should work. assuming you have the folder containing image inside a resource/source folder. – Coding Otaku Dec 03 '19 at 08:27
  • @RahulS i've tried this as well but the same NullPointerException, you can watch my project's structure on the post – jonathanmarq Dec 03 '19 at 09:43
  • i've tried to compile the project on ubuntu's command line and i've added the result on the post, hope it helps :( apparently can't find the `getResources()` metod on the class – jonathanmarq Dec 03 '19 at 09:47
  • btw: there's no need to show screenshots of stacktraces (nor code), just copy and paste the text. – kleopatra Dec 03 '19 at 10:54
  • it certainly does find the method - what it doesn't find is the resource :) Please read the api doc of getResource very carefully: it's looking up a _package path_, either relative or absolute from the class. So if it doesn't find it, the resource is not where you expect it to be (see @fabian's comment as to where it should be) - check your output hierarchy to see if it is copied over. – kleopatra Dec 03 '19 at 11:05
  • @kleopatra i'm sorry i think fabian's comment isn't here anymore. can you help me? i've tried like a thousand times that the resources are where it suppoused to be. I've even tried to move the directory `resources` inside of `vista/aplicacion` where my main class is and into `vista/pestanas` where the class that needa to show the image is but every time i get the same Exception – jonathanmarq Dec 03 '19 at 19:26
  • @jonathanmarq would you mind [listing out all resources from classpath](https://stackoverflow.com/questions/3923129/get-a-list-of-resources-from-classpath-directory)? Or you could just export it as a jar and see where the images are present. if it is not present then you need to double check the resource folder is actually being exported or not. – Coding Otaku Dec 05 '19 at 07:58

1 Answers1

0

Going to assume you are using JDK 8 and above. The path must be relative and be preceded with the appropriate platform path separator.

Define an ImageProvider to ensure you handle the absence of the image correctly. Note that I use a class reference to establish the resource path.

public class ImageProvider {
        private static ImageProvider imageSingleton;
        private Image image = null;
        private static final java.util.logging.Logger LOGGER = LogManager.getLogManager().getLogger("MyApp");

        private ImageProvider(String imageStr) {
            try {
                image = new Image(ImageProvider.class.getResource(imageStr).toURI().toString());
            } catch (URISyntaxException ex) {
                LOGGER.log(Level.SEVERE, "Cannot fine image.", ex);
            }
        }
     /**
     * Returns an Image if possible.
     * @param imageStr the relative path preceded by the appropriate platform path separator.
     * @return
     */
    public static Image getImage(String imageStr) {
        return Optional.ofNullable(imageSingleton).orElseGet(() -> imageSingleton = new ImageProvider(imageStr)).image;
    }

You can then use;

ImageProvider.getImage("/img/myImage.png")
cbm64
  • 1,059
  • 2
  • 12
  • 24
  • for my code, throws the same Exception. I suppose that the String that provides the path is wrong but i've tried every path that supposed to make sense, as you can check up there, and even change the location of the directory but nothing works – jonathanmarq Dec 03 '19 at 19:38
  • I just looked at the first path you have for your image, 'src/vista/resources' is not right. It should be 'src/main/resources' – cbm64 Dec 03 '19 at 21:21