Look at your code:
public Image loadImage(File file)
{
try
{
Image image = ImageIO.read(file);
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
return image;
}
And think about scoping rules: where is
image
defined? Where are you using it?
Because you declare
image
inside the
try
block, it goes out of scope at the matching close curly bracket, so it isn't available when you hit the
return
statement.
Move the initial declaration outside the
try
block, and you'll be fine:
public Image loadImage(File file)
{
Image image = null;
try
{
image = ImageIO.read(file);
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
return image;
}