2
public Image  images[] = new Image[20];

    for(i=0; i<10; i++){
images[i]=new Image(getClass().getResource("/images/"+i+".jpg"));
        }

I am trying to add images to array but it gives the error Cannot instantiate the type Image j

What can be the reason?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
john brown
  • 55
  • 1
  • 1
  • 5

2 Answers2

6

Abstract classes cant be instantiated directly. You could use ImageIO.read which returns BufferedImage, a sub-class of Image

void loadImages() throws IOException {

    for (int i = 0; i < 10; i++) {
        images[i] = ImageIO.read(getClass().getResource("/images/" + i + ".jpg"));
    }
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Multiple markers at this line - Unhandled exception type IOException - Cannot instantiate the type Image – john brown Sep 23 '13 at 13:42
  • `IOException` is a checked exception, which needs a try/catch block or thrown from the surrounding method as shown – Reimeus Sep 23 '13 at 13:56
4

Image is an abstract class and thus cannot be instancied. You should use one of the class who extend Image, like BufferedImage or VolatileImage.

Source: Javadoc of Image

Julien
  • 2,256
  • 3
  • 26
  • 31