views:

152

answers:

2

Error: Unhandled exception type IOException.

File imgLoc = new File("player.png");
BufferedImage img = ImageIO.read(imgLoc);

How do I get a bufferedImage from a file location?

+2  A: 

Does the file exist ? Are you by chance reading from an unexpected directory ?

Try File.exists() and/or File.canRead()

Brian Agnew
+3  A: 

The cause of your problem is best determined by examining a stacktrace for the exception.

As a temporary measure, replace those two lines with the following:

File imgLoc = new File("player.png");
BufferedImage img;
try {
   img = ImageIO.read(imgLoc);
} catch (IOException ex) {
   System.err.println(ex.getMessage());
   ex.printStackTrace();
   throw ex;
}

to send some diagnostics to standard error. Run the modified app and post the resulting output.

Possible causes include:

  • The file name is wrong,
  • The file is not in the app's current directory,
  • The file is not readable by the app due to operating system access controls,
  • The file is readable but there is something wrong with its format,
  • etcetera.
Stephen C