使用ImageIO.read获取错误(getClass()。getResourceAsStream(input == NULL)? [英] Getting error with ImageIO.read(getClass().getResourceAsStream (input==NULL)?

查看:453
本文介绍了使用ImageIO.read获取错误(getClass()。getResourceAsStream(input == NULL)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用以下代码时,我不断收到此错误:

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;
}

它一直给我input == null错误。

我已经尝试找到一个解决方案(几个已回答的问题已经有了实际的解决方案,但那些对我不起作用)但我还没有运气好的话。例如,这一个非常类似于我的问题,但我似乎无法确切地找到放置我的图像文件的位置。

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.

我注意到大部分都是通过简单的文件放置解决的,因此,当我在NetBeans上构建和src时,某些人拥有bin会让人感到更加困惑。

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...

推荐答案

我假设,从名称上, build / classes / 是类路径中的文件夹;所以你可能需要写的是:

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"));






编辑以下评论:由于 javaproject / 实际上不在 build / classes / 内,我猜你确实需要:


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

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

(我知道这里的上下文有点不同,但应该清楚这个系统是建模的有点在文件系统上。如果你的 build / classes / 目录不包含 javaproject / 目录,那么为什么呢?你有没有想过写 build / classes / javaproject / ?)

(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/?)

这篇关于使用ImageIO.read获取错误(getClass()。getResourceAsStream(input == NULL)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆