IllegalArgumentException Input == null

心已入冬 提交于 2019-12-14 03:12:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!