Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ImageIO.read return null

 
Zeshan Kha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code but it won't work and I wonder why? I will appreciate if somebody can help me out here! Thanks!!

public class LoadImages extends JPanel {

BufferedImage img = null;


public LoadImages(){

try{

img = ImageIO.read(new File( "craft.png"));

}catch (IOException e){}

}

@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(img,0,0,this);

}



}//end LoadImages


 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most likely the file doesn't exist.

If you think it does, then most likely you haven't considered what the current working directory is when you run that code. The file isn't in that directory.

There's also the possibility that an exception is thrown for some other reason. Your code is specifically written to ignore such an exception, so you would be unable to find that out. Change it so that it at least tells you about exceptions:
 
Zeshan Kha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The image file is in the same directory. I followed your advice and got the following error.

javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)

 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then that means the image classes can't read the file. Perhaps it isn't an image file of the kind which they support.
 
Zeshan Kha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange I run the program in bluej editor it works but in netbeans it doesn't. Hummmmm

thanks for your help. I really appreciate it.
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you don't know what your current working directory is in the two IDEs. Just saying it's "in the same directory" isn't helpful.
 
Zeshan Kha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right!

I fixed it. Thanks a lot!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic