aspose file tools*
The moose likes Beginning Java and the fly likes Reading an image from a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading an image from a file" Watch "Reading an image from a file" New topic
Author

Reading an image from a file

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

This code won't work, has anything changed for Java 1.6?


Error message
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:161)
at ImageReader.main(ImageReader.java:29)



It's like it can't find the image, I have placed image.gif, and, image.jpg within the same directory where the source code is compiled and run, and, I have tried compiling on Eclipse 3.6.2, and, on TextPad. Also, any idea which image formats (e.g. jpg, gif, png etc) does Java and which libraries support reading these formats?

Code sourced from an article at http://www.exampledepot.com/egs/javax.imageio/BasicImageRead.html.





Jon
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
I don't think anything has changed with ImageIO. It supports GIF, JPEG, PNG, BMP and WBMP (and can be made to support TIFF, RAW, JPEG2000 and PNM with little effort).

You should never do this, though:

} catch (IOException e) {
}

At least print out an error message so you know if something went wrong.


Ping & DNS - my free Android networking tools app
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Ulf Dittmer wrote:I don't think anything has changed with ImageIO. It supports GIF, JPEG, PNG, BMP and WBMP (and can be made to support TIFF, RAW, JPEG2000 and PNM with little effort).

You should never do this, though:

} catch (IOException e) {
}

At least print out an error message so you know if something went wrong.



Can't read input file!
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ImageReader.main(ImageReader.java:29)

The input file is available online.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
Both code variants work fine for the JPEG file. If it's specifically the GIF file you're having trouble with, you can attach it to a response here.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Ulf Dittmer wrote:Both code variants work fine for the JPEG file. If it's specifically the GIF file you're having trouble with, you can attach it to a response here.


A link is available in my previous post, but it does not work on my laptop somehoe...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
A link is available in my previous post

You posted a link to a JPEG file, not a GIF file (which is what the code you posted is trying to open).
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Ulf Dittmer wrote:
A link is available in my previous post

You posted a link to a JPEG file, not a GIF file (which is what the code you posted is trying to open).


Sorry, the .gif version is available at this other link...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
Can't reproduce it. Both variants work fine for me for both images.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Ulf Dittmer wrote:Can't reproduce it. Both variants work fine for me for both images.


Thanks, what OS are you using? What is your Java version? Are you using Eclipse or compiling through the command line?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
Java 1.6 on OS X. Command line; if you're using an IDE then its idea of what the current directory is may be different from your idea of what it might be.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Ulf Dittmer wrote:Java 1.6 on OS X. Command line; if you're using an IDE then its idea of what the current directory is may be different from your idea of what it might be.


It's working now with TextPad, I was calling different filenames in my code

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reading an image from a file