0

I need to create an array of images. when I try to do the getGraphics method of the Graphics class to access the Image I created in the array index it throws the java.lang.NullPointerException error even though a few lines above I created the image in that index

I tried changing the index and changing the condition of the foor loop, but it still enters the loop only once and says that the array in index 0 is null.

private Image[] frames = new Image[100];
for (int index = 0; index < frames.length; index++){
        if (frames[index] == null) 
        {
            frames[index] = this.createImage(SCREEN_WIDTH, SCREEN_HEIGHT);
        }

        Graphics graphic = frames[index].getGraphics();

I expect the array to be full of images at the end, in order to paint them and finish the job.

Kyle Williamson
  • 2,251
  • 6
  • 43
  • 75
jorgeG
  • 1

1 Answers1

-1

you can use following code:

ArrayList array = new ArrayList ();

and you can add Image like this:

array.add(BitmapFactory.decodeFile("file_path")); //there is many Prototypes for decodeFile

then you can use Bitmap like this:

ImageView image = (ImageView) findViewById(R.id.ImageView); image.seImageBitmap(array.get(0));