问题
Error thrown--
Exception in thread "Thread-3" java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source)
Code--
try {
playerImage = ImageIO.read(Player.class
.getResourceAsStream("/toon.png"));
} catch (IOException e) {
e.printStackTrace();
}
File path--
H:\workspace\Isaac\resources\toon.png
Ive researched this a bit but none of the solutions have really worked for me. any ideas?
回答1:
If Isaac
is your project folder, then the way your are reading should work. Please check whether the directory resources
is a source folder (in eclipse it should have an icon of a folder with an overlay of a java package), if not then right click on it and got to Build path
and choose Use as Source Folder
.
Your directory structure should look something like this:
before compiling
H:\workspace\Isaac
├───resources
│ toon.png
│
└───src
Player.java
after compiling
H:\workspace\Isaac
├───bin
│ Player.class
│ toon.png
│
├───resources
│ toon.png
│
└───src
Player.java
getResourceAsStream
looks for the image in bin
and not in the one in resources
来源:https://stackoverflow.com/questions/27215235/imageio-wont-import-bufferedimage-correctly