This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Game Development and the fly likes ImageIO.read return null Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Game Development
Reply 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: 16479
    
    2

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: 16479
    
    2

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: 16479
    
    2

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!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ImageIO.read return null
 
Similar Threads
Displaying a png on screen using AWT
casting
Image Overlapping not proper
Is there a better way to blend colors when painting with Graphics2D?
Add image to JPanel