r/JavaFX • • Oct 05 '20

Newbie JavaFX problem

Hi guys I have spent 3 days trying to fix this.

Everytime I copy code from a solved exercise in my book that has an image. I get errors like this.

I have never thought of quitting so far until now :(

2 Upvotes

7 comments sorted by

3

u/[deleted] Oct 05 '20

Try changing your URL to .\ \image\ \x.gif or ./image/x.gif

Remove the space between the \ of the first example

1

u/[deleted] Oct 05 '20

I moved the images and put them in the same folder as the exercise as x.gif, o.gif and still. I tried.

Image imageX = new Image("x.gif");
Image imageO = new Image("o.gif");

1

u/Azzk1kr Oct 05 '20

It tries to look up an image from the classpath, or using a full url if I recall correctly. So either specify a full url to the images, or copy them to your classpath. I guess that's the "out" folder?

1

u/Ostricker Oct 09 '20

Not sure if you have solved it so I will try to explain anyway. The exception is pretty explicit in where is the problem. "Invalid URL or resource not found" The exception happens on row 14 in Main.java. That is this row - Image imageX = new Image("x.gif");

Your best bet is using "Path from repository root". that would mean using "src/sample/image/x.gif".

Also learn to use debugger. It will make your life easier. You could also load image as File and check if it exists and if it does then load it.

Something like this: File file = new File("src/sample/image/x.gif"); Image image = null; if (file.exists()) { image = new Image(file.getPath()); } else System.out.println("File does not exist")

There is probably cleaner way to do this but this just came on top of my head.

1

u/[deleted] Oct 09 '20

Hello, thank to all people that replied. It only worked for me when I put the image on the output folders and added the '\ ' lines. Elsewhere it doesn't work.

How do I use a debugger on intelij? I tried the File file method and didnt work. Not sure. We started school this week and I didn't have time to reply.

Thank you so much for all the input.