-1

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

Community
  • 1
  • 1
  • 1
    Re: "I noticed most of them were solved by simple file placement": This is perhaps an indication that your question should say something about file placement . . . – ruakh May 11 '14 at 20:51
  • 1
    It seems *very* unlikely that you've actually got a folder called "build" *within* your classpath, and relative to the class. I strongly suspect that `getClass().getResourceAsStream("Space.gif")` would be more likely to work, but we really don't know nearly enough about what class you're calling this from or how your files are laid out to help you. More information required. – Jon Skeet May 11 '14 at 20:51
  • The thing is I am unsure how to answer this, there is the "build","src","nbproject" and "test" folders. I have the image in the build folder, where the classes are located. – user3624649 May 11 '14 at 20:57
  • Here is the arrangement in my code: http://oi60.tinypic.com/2nvfin5.jpg – user3624649 May 11 '14 at 21:03
  • @Jon Skeet Space.gif is something I tried earlier as well, no dice – user3624649 May 11 '14 at 21:05
  • @user3624649: Well I was having to guess, as you hadn't given us the relevant information. Now we can see where the file is, ruakh's answer should help you. Next time, please give all the relevant information from the start. – Jon Skeet May 11 '14 at 21:07
  • @Jon Skeet Is it a bad thing if it still doesn't work? The file is in the same place as the image, and I changed it to "/Space.gif", but it still gives the illegal argument exception – user3624649 May 11 '14 at 21:10
  • @user3624649: Well it looks like it should be `/Space_1.gif` based on your screenshot... – Jon Skeet May 11 '14 at 21:14
  • @Jon Skeet Noticed that, a spark of hope faintly crossed my mind but it kept failing after changing it to Space.gif. – user3624649 May 11 '14 at 21:15

2 Answers2

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

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Tried this, didn't solve the issue. The image path is JavaProgram -> build -> classes -> Space.gif – user3624649 May 11 '14 at 20:58
  • Well I did have the image file in the javaproject folder of "classes", as you can see by this pic http://oi60.tinypic.com/2nvfin5.jpg. I have changed it now, but "/Space.gif" still doesn't seem to work. I wonder if this is beyond just file placement... – user3624649 May 11 '14 at 21:08
0

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

ClassLoader.getSystemResourceAsStream