Getting error with ImageIO.read(getClass().getResourceAsStream (input==NULL)?

半世苍凉 提交于 2019-12-20 07:47:18

问题


I keep getting this error when I use the following code:

try    
{
      image=ImageIO.read(getClass().getResourceAsStream("build/classes/javaproject/Space.gif"));
}

catch (IOException ex) 
{
      lastException=ex;
}

It gives keeps giving me the "input==null" error.

I have already tried to find a solution to this (several answered questions already had the solution actually, but those didn't work for me) but I haven't had any luck. This one for example was very similar to my problem, but I can't seem to find out exactly where to put my image file in.

I noticed most of them were solved by simple file placement, and so it's all the more confusing when some people have 'bin' while I have 'build' and 'src' on NetBeans.

Also, I realize the path is probably erroneous, but I already tried it many different ways and it hasn't seemed to work with any of them. I would really appreciate any help...


回答1:


I am assuming, from the name, that build/classes/ is a folder on your classpath; so what you probably need to write is:

      image=ImageIO.read(getClass().getResourceAsStream("/javaproject/Space.gif"));

Edit for comment below: Since javaproject/ is not actually inside build/classes/, I guess you actually need:

      image=ImageIO.read(getClass().getResourceAsStream("/Space.gif"));

(I know the context here is a bit different, but it should be clear that this system is modeled somewhat on a filesystem. If your build/classes/ directory doesn't contain a javaproject/ directory, then why would it ever occur to you to write build/classes/javaproject/?)




回答2:


The promblom is that its A system file so you got too do this:

ClassLoader.getSystemResourceAsStream



来源:https://stackoverflow.com/questions/23597763/getting-error-with-imageio-readgetclass-getresourceasstream-input-null

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