0

Hey i am currently trying to read the byte contents of an image into a byte array in java but cant seem to open the image from file. The console is throwing this exception:

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

This here is the line in question:

BufferedImage im =
   ImageIO.read(
      new File(
         getClass().getResource( "/Images/default_pic.png" ).toURI()));

My packages look like this:

-UI
  -Class //code getting image here
-UI.Images
  -default_pic.png
Aerospace
  • 1,270
  • 12
  • 19
user1851487
  • 547
  • 5
  • 13
  • 28
  • The easiest way to debug a name resolution problem is to set a breakpoint on your `ImageIO.read()` and try a lot of different paths until `getResource()` or `getResourceAsStream()` returns something non-null. You can just change the code while the debugger is waiting, then select the `getClass().getResource()` method call and right-click "Inspect" to see the value. In general, Java name resolution is unpredictable and unreliable--there are no actual rules to it--so you just have to explore possibilities by trial-and-error until you hit the spot. – Byron Hawkins Feb 23 '13 at 08:22

3 Answers3

2

The directory UI is missing in your path, I suggest:

BufferedImage im =
   ImageIO.read(
      getClass().getClassLoader().getResourceAsStream(
         "/UI/Images/default_pic.png" ));
Aerospace
  • 1,270
  • 12
  • 19
  • thanks for the reply, after trying yours i also got an java.lang.IllegalArgumentException: input == null! exception – user1851487 Feb 22 '13 at 11:37
1

You can try this:

BufferedImage im = ImageIO.read(getClass().getClassLoader().getResourceAsStream("/Images/default_pic.png"))
vikingsteve
  • 38,481
  • 23
  • 112
  • 156
0

Check if the file uses CMYK coloring (Don't know if this only applies to jpegs). If that's the case then ImageIO can't read it without an extension.

Check http://www.randelshofer.ch/blog/2011/08/reading-cmyk-jpeg-images-with-java-imageio/

Marcus
  • 1,866
  • 1
  • 20
  • 33