0

I have an arrayoutofindexexception in my code below:

    ImageView[] bObject = new ImageView[9];

    private void getCorrectObject() {

    List<Integer> objects = new ArrayList<Integer>();
    objects.add(1);
    objects.add(2);
    objects.add(3);
    objects.add(4);
    objects.add(5);
    objects.add(6);
    objects.add(7);
    objects.add(8);
    objects.add(9);

    Collections.shuffle(objects);
    int correctObject =  objects.get(0);
    Log.d("test", String.valueOf(correctObject));

            bObject[correctObject + 1].setImageResource(R.drawable.stage1_4_object1);
    bObject[correctObject + 1].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                goToNextQuestion();
            }
        });
            }


           //TODO initControls
private void initControls() {

    bObject[0] = (ImageView) findViewById(R.id.bObject1Stage1_4);
    bObject[0].setImageResource(R.drawable.stage1_4_object2);
    bObject[1] = (ImageView) findViewById(R.id.bObject2Stage1_4);
    bObject[1].setImageResource(R.drawable.stage1_4_object2);
    bObject[2] = (ImageView) findViewById(R.id.bObject3Stage1_4);
    bObject[2].setImageResource(R.drawable.stage1_4_object2);
    bObject[3] = (ImageView) findViewById(R.id.bObject4Stage1_4);
    bObject[3].setImageResource(R.drawable.stage1_4_object2);
    bObject[4] = (ImageView) findViewById(R.id.bObject5Stage1_4);
    bObject[4].setImageResource(R.drawable.stage1_4_object2);
    bObject[5] = (ImageView) findViewById(R.id.bObject6Stage1_4);
    bObject[5].setImageResource(R.drawable.stage1_4_object2);
    bObject[6] = (ImageView) findViewById(R.id.bObject7Stage1_4);
    bObject[6].setImageResource(R.drawable.stage1_4_object2);
    bObject[7] = (ImageView) findViewById(R.id.bObject8Stage1_4);
    bObject[7].setImageResource(R.drawable.stage1_4_object2);
    bObject[8] = (ImageView) findViewById(R.id.bObject9Stage1_4);
    bObject[8].setImageResource(R.drawable.stage1_4_object2);

            for (ImageView bObj  : bObject) {
                   bObj.setOnClickListener(this);
            }

I'm getting an error in this line:

        bObject[correctObject + 1].setImageResource(R.drawable.stage1_4_object1);

I don't know what's wrong with this. If you find what's wrong, any help is truly appreciated. Thanks

3 Answers 3

5

Arrays are zero based. Here the values used exceed the bounds of the array bObject. Use

Add values to the List starting from 0:

objects.add(0);
...
objects.add(8);

and use:

bObject[correctObject].setImageResource(...);

Alternatively, you can use:

for (int i=0; i < bObject.length; i++)
   objects.add(i);
}

This will remove the need to hard code index values.

To eliminate the objects index List completely, Random#nextInt can be used, for example:

bObject[random.nextInt(bObject.length)].setImageResource(...);
Sign up to request clarification or add additional context in comments.

2 Comments

Okay so what happens to the correctObject? Since I set it equal to objects(0);?
There is no issue. After the shuffle you are retrieving the value at index 0. Once the values are with the index range an ArrayOutOfIndexException will not occur
0

Arrays are 0 based in Java- the first index is 0. So bObject has valid range of 0...8. The value of correctObject will be random 1...9. Adding 1 to it will bring 2...10. So if you get a 9 or 10 you're accessing out of range of the array. A 20% chance you'll do that and crash.

You probably wanted to subtract 1, not add it.

Comments

0

You should say bObject[correctObject - 1].setImageResource(R.drawable.stage1_4_object1);

because correctobject value could be 9 and max index you could give to bObject is 8.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.