• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading an image from a file

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't reproduce it. Both variants work fine for me for both images.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic