-1

I am getting crashes of ArrayIndexOutOfBoundsException on google developer console on some devices. It is strange for me because it does not happen on my device and some others which i have tested and i also tested my apk on different versions of API'S on emulator and crash does not happen. I am totaly blind what should i do to avoid this crash as i am unable to see it.Getting crash on button_array[btn_ref] = new Button(mContext); Attached below is my code for button array:

public static Button[] button_array = null;
int btn_ref = 0;

int count = 0;
        for (int i = 0; i < words.size(); i++) {

            if (i >= 5) {

                for (int z = i; z < words.size(); z++) {
                    bonus_words.add(words.get(z));
                }
                if (!checkResult.equals("true")) {
                    tinydb.putListString("BonusList", bonus_words);
                }
                break;
            } else {
                char[] eachLetterinArray = words.get(i).toCharArray();
                for (int j = 0; j < eachLetterinArray.length; j++) {
                    if (Character.isLetter(eachLetterinArray[j])) {
                        count++;
                    }
                }
            }

        }

button_array = new Button[count];

 for (int i = 0; i < words.size(); i++) {
            int z = words.get(i).length();

            LinearLayout ln = new LinearLayout(mContext);
            ln.setOrientation(LinearLayout.HORIZONTAL);
            ln.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

            row = i;
            int row_items_count_start = 0;
            int row_items_count_end = 0;

            boolean first_time_check_row = true;

for (i = 0; i < z; i++) {

                row_items_count_end = btn_ref;
                if (first_time_check_row) {
                    row_items_count_start = btn_ref;
                    first_time_check_row = false;
                }

                button_array[btn_ref] = new Button(mContext); //**Getting crash here**

                LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(width_height_cal, width_height_cal, 0.1f);
                p.setMargins(5, 5, 5, 5);
                button_array[btn_ref].setLayoutParams(p);
                button_array[btn_ref].setTextColor(Color.WHITE);
                button_array[btn_ref].setBackgroundResource(R.drawable.un_filled_box2);
                ln.addView(button_array[btn_ref]);

                btn_ref++;
            }

            indexes.add("row " + row + "," + row_items_count_start + "," + row_items_count_end + "," + words.get(i));
            vert_lay.setOrientation(LinearLayout.VERTICAL);
            vert_lay.addView(ln);

            if (indexes.size() >= 5) {
                break;
            }

btn_ref is used to save index of a button in array and after that i am putting that button array into a layout

  • Are you trying to create `z` about of buttons? – Cooper Scott Nov 02 '20 at 06:16
  • I have updated my question. Actually i have an arraylist of ```words``` and i am fetching words one by one in the top loop and z contains the length of each word one by one and after that button array is created for the words – Moneeb Ahmed Nov 02 '20 at 06:24

1 Answers1

0

You must initialize an array with size

int max_word_size = 20; //for example
public static Button[] button_array = new Button[max_word_size ];
  • I have updated my code. Actually i am getting first 5 entries of words array and then get size of each word and increment count on each letter. button_array is having a dynamic size passed to it i.e count – Moneeb Ahmed Nov 02 '20 at 06:59
  • array has fixed size. For dynamic size use arraylist or use an array with max supposed size – Ann Developer Nov 02 '20 at 07:06
  • is there any difference between using this simple array Button[] button_array = new Button[max_word_size]; and ArrayList – Moneeb Ahmed Nov 02 '20 at 07:47
  • array has a fixed size. We cant change it after create. But arraylist we can remove items, add new items, change size for new task. Arraylist is more flexible – Ann Developer Nov 02 '20 at 08:00
  • i am not changing the size of array. i am giving it a fixed size on runtime and after that populating that array. This process leads to crashes on some devices displayed on developer console – Moneeb Ahmed Nov 02 '20 at 08:59