aspose file tools*
The moose likes Game Development and the fly likes ImageIO.read return null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "ImageIO.read return null" Watch "ImageIO.read return null" New topic
Author

ImageIO.read return null

Zeshan Kha
Greenhorn

Joined: Feb 11, 2012
Posts: 4
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
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

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

Joined: Feb 11, 2012
Posts: 4
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
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

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

Joined: Feb 11, 2012
Posts: 4
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
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

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

Joined: Feb 11, 2012
Posts: 4
Yes, you are right!

I fixed it. Thanks a lot!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ImageIO.read return null