user3213132
user3213132

Reputation: 3

ArrayIndexOutOfBounds, can't figure why?

i wrote this method:

public static Bitmap matrixToBitmap(int[][] slika)
     {

         int w = slika[0].length;
         int h = slika[1].length;

         Bitmap into = Bitmap.createBitmap(w, h, Config.ARGB_8888);
         for (int x = 0; x < w; x++)
         {

             for (int y = 0; y < h; y++)
             {
                 if(slika[x][y] < 128)
                     into.setPixel(x, y, Color.BLACK);
                 else
                     into.setPixel(x, y, Color.WHITE);

             }
         }

         return into;
     }

and when I call it inside my android app with an int[454][454] array, it says this in Logcat:

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=452; index=452

pointing at this line of matrixToBitmap method:

     if(slika[x][y] < 128)

Can someone figure why is it happening? Values for w and h become 454 and 454, just as they should be.

Upvotes: 0

Views: 159

Answers (1)

user902383
user902383

Reputation: 8640

error is here:

int w = slika[0].length;
int h = slika[1].length;

what happened is, you set up length of first row from your array to be w, and length of your second row to be h

to make it work, change it to:

int w = slika.length;
int h = slika[0].length;

Upvotes: 2

Related Questions