-1

I get the following error when I try to load a buffered image in a junit test.

Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor

The code this affects is:

BufferedImage testFrame = ImageIO.read(new File("C:/Users/Darren/testPicture.png"));

I have tried surrounding it with try and catch. I get an error stating incorrect syntax when I use try catch.

I have no errors when using buffered image in my main program.

Any help would be great.

1 Answers1

0

Try-catch is the solution here. This example has correct syntax:

BufferedImage testFrame;
try {
    testFrame = ImageIO.read(new File("C:/Users/Darren/testPicture.png"));
} catch(IOException e) {
    // do something about it
}

The fact that testFrame is defined outside the try-catch is very important. If not, testFrame will go out of scope after the try block.

tbodt
  • 16,609
  • 6
  • 58
  • 83