-
Can't load images
I've looked throughout the internet on ways to modify images, but my biggest problem is that I can't load images unless I specify the full path! I have the image in the same directory as the class & java files. I use eclipse, and it gives a qualified name, or I use just the base name like tutorials do, but I always get a null pointer exception.
Code:
Image img;
Toolkit tool = Toolkit.getDefaultToolkit();
img = tool.getImage("test.png");
Exception:
Code:
Exception in thread "Thread-4" java.lang.NullPointerException
at com.ityguy32.AppletGame.ImageReader.<init>(ImageReader.java:18)
at com.ityguy32.AppletGame.Start.run(Start.java:22)
at java.lang.Thread.run(Unknown Source)
-
-
You need to know where Java is looking for the file, and likely it's the user's working directory. To find out what this is, place this somewhere in a program in the same package as your current program:
Code:
System.getProperty("user.dir");
The other option is not to use files at all but instead use resources. If you do that, you'll likely find your images easily since resources look in the location of where your class files are located.