2

When executing following code, imgURL is always set to NULL:

try {
    URL imgURL = getClass().getResource("assets/image.png");
    System.out.println(imgURL);
    BufferedImage sheet = ImageIO.read(imgURL);
} catch (IOException e) {
    System.out.println("Image failed to load.");
    e.printStackTrace();
}

Assets is a source folder, and is listed under "Project>Properties>Java Build Path". I have also tried to clean the project and rebuild. Additionally, adding a "/" before "assets" does not work and would not fulfill my purposes anyways.

This is a previous version of the code which worked in eclipse, but not as an executable Jar:

BufferedImage sheet = ImageIO.read(new File("assets/image.png"));

I simply don't understand why imgURL is being set to NULL in the first place, as the predominant answer online is to use the exact syntax as in the first code block. This has also been implicated for use in executable JAR files, which my project will require for thorough testing.

Any help you can give will be much appreciated.

Thanks!

EDIT: My problem has been solved thanks to TheLostMind and ingrid. As it turns out, when using getClass().getResource(filename), I needed to be using the file name without the relative path (assets/filename).

Community
  • 1
  • 1

0 Answers0