问题
I am trying to read an Image as an InputStream
. But for some reason I always get an IllegalArugmentException
.
Here is my code:
BufferedImage i = null;
i = ImageIO.read(getClass().getResourceAsStream("/res/graphics" + path));
回答1:
Reason:
Your resource evaluates to null
and that is why the exception
API doc
Throws: IllegalArgumentException - if input is
null
.
Solution:
If res/graphics/whatever
is in classpath at root then it will return not null
回答2:
Basically, the resource with the name does not exist. The resource is located by the class loader in the same way as a class with the name res.graphics.whatever
, with of course a more relevant whatever. So just use the same methods to make a class accessible to make this resource accessible.
回答3:
Seems "/res/graphics" + path
doesn't evaluate to your desired value.
Below is how you use BufferedImage
,
public File myImg = new File("someImage.png");
BufferedImage in = ImageIO.read(myImg);
//Just an example
BufferedImage newImage = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);
来源:https://stackoverflow.com/questions/17905496/illegalargumentexception-input-null