-1

I've been searching for quite a while on why I kept getting this error even though the path to my image was set correctly.

In IntelliJ, right click on your "resources" folder and press "Mark directory as" > "Resources root"

Then, when referring to your image, you can use this code:

Image myImage = new Image("myImage.jpg");

As you can see, this appeared to be set up correctly, but somehow that still gave me this error.

Edit: This is not a question on how to get the correct file path, as the file path is correct. It's more a question (& answer) on how to resolve this build error. Therefore the duplicate is invalid in my opinion.

Jonas
  • 82
  • 9
  • Also see the [eden resource guide](https://edencoding.com/where-to-put-resource-files-in-javafx/) referenced in the answer to the duplicate. – jewelsea Feb 24 '22 at 12:46
  • "I kept getting this error even though the path to my image was set correctly" -> The path you are using is incorrect, otherwise you will not see the message "Invalid URL or resource not found". Follow the troubleshooting steps in the duplicate answer. – jewelsea Feb 24 '22 at 12:47
  • I advise using a build tool. You are using Idea, so [create a new JavaFX project](https://www.jetbrains.com/help/idea/javafx.html), and place the image in the resource directory (the new project will have this setup automatically, you don't need to manually configure it), following one of the suggested location patterns from the eden guide and perform the location lookup according to the guide. – jewelsea Feb 24 '22 at 12:51
  • @jewelsea How come I could just rebuild the project and that fixed it then? If the path is incorrect, rebuilding shouldn't help, right? – Jonas Mar 01 '22 at 20:20
  • "How come I could just rebuild the project and that fixed it then? If the path is incorrect, rebuilding shouldn't help, right?" -> I don't know. If it was already built, then rebuilding without a configuration change shouldn't make a difference as you say. It is possible that Idea indices got corrupted somehow, so forcing a rebuild fixed the issue, but other than that I couldn't say. – jewelsea Mar 01 '22 at 21:00

1 Answers1

1

It looks like the image somehow wasn't added to the build, so even though it was in the project's code, it wasn't in the build, thus it couldn't be referred to.

These steps resolved the issue:

  1. Remove the "out" folder
  2. Rebuild the project by clicking the greene hammer on the top right of the screen
  3. Try to re-run the project, and it should work! :)

Illustration of the "out" folder

Jonas
  • 82
  • 9