Question

I'm trying to get random values from a Arraylist and then storing it in Buttons. Every time when I run this code with the buttonListRbra my app crashes.

Here is my code:

    private void generateRandomNumbersRbra()
 {
  int rnd;
  Random rand = new Random();
  int[] randomPick = new int[8];
  ArrayList<Integer> numbersRbra = new ArrayList<Integer>();      
  Button[] buttonListRbra = new Button[8];
  buttonListRbra[0] = (Button)findViewById(R.id.tap1);
  buttonListRbra[1] = (Button)findViewById(R.id.tap2);
  buttonListRbra[2] = (Button)findViewById(R.id.tap3);
  buttonListRbra[3] = (Button)findViewById(R.id.tap4);
  buttonListRbra[4] = (Button)findViewById(R.id.tap5);
  buttonListRbra[5] = (Button)findViewById(R.id.tap6);
  buttonListRbra[6] = (Button)findViewById(R.id.tap7);
  buttonListRbra[7] = (Button)findViewById(R.id.tap8);
  buttonListRbra[8] = (Button)findViewById(R.id.tap9);    
  for (int i=0 ; i< 8;i++)
  {
   rnd = rand.nextInt(8) + 1;

   if(i==0)
   {
    randomPick[0] = rnd;
    numbersRbra.add(randomPick[0]);
   }
   else
   {
    while(numbersRbra.contains(new Integer(rnd)))
    {
     rnd = rand.nextInt(8) + 1;
    }
    randomPick[i] = rnd;
    numbersRbra.add(randomPick[i]);
   }
  }
 }

logCat:

05-07 14:15:50.325: D/libEGL(10749): loaded /system/lib/egl/libEGL_mali.so 05-07 14:15:50.355: D/libEGL(10749): loaded /system/lib/egl/libGLESv1_CM_mali.so 05-07 14:15:50.360: D/libEGL(10749): loaded /system/lib/egl/libGLESv2_mali.so 05-07 14:15:50.370: E/(10749): Device driver API match 05-07 14:15:50.370: E/(10749): Device driver API version: 23 05-07 14:15:50.370: E/(10749): User space API version: 23 05-07 14:15:50.370: E/(10749): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 9 21:05:57 KST 2013 05-07 14:15:50.490: D/OpenGLRenderer(10749): Enabling debug mode 0 05-07 14:15:54.010: D/AndroidRuntime(10749): Shutting down VM 05-07 14:15:54.010: W/dalvikvm(10749): threadid=1: thread exiting with uncaught exception (group=0x41bdc700) 05-07 14:15:54.015: E/AndroidRuntime(10749): FATAL EXCEPTION: main 05-07 14:15:54.015: E/AndroidRuntime(10749): java.lang.IllegalStateException: Could not execute method of the activity 05-07 14:15:54.015: E/AndroidRuntime(10749): at android.view.View$1.onClick(View.java:3838) 05-07 14:15:54.015: E/AndroidRuntime(10749): at android.view.View.performClick(View.java:4475) 05-07 14:15:54.015: E/AndroidRuntime(10749): at android.view.View$PerformClick.run(View.java:18786) 05-07 14:15:54.015: E/AndroidRuntime(10749): at android.os.Handler.handleCallback(Handler.java:730) 05-07 14:15:54.015: E/AndroidRuntime(10749): at android.os.Handler.dispatchMessage(Handler.java:92) 05-07 14:15:54.015: E/AndroidRuntime(10749): at android.os.Looper.loop(Looper.java:176) 05-07 14:15:54.015: E/AndroidRuntime(10749): at android.app.ActivityThread.main(ActivityThread.java:5419) 05-07 14:15:54.015: E/AndroidRuntime(10749): at java.lang.reflect.Method.invokeNative(Native Method) 05-07 14:15:54.015: E/AndroidRuntime(10749): at java.lang.reflect.Method.invoke(Method.java:525) 05-07 14:15:54.015: E/AndroidRuntime(10749): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 05-07 14:15:54.015: E/AndroidRuntime(10749): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 05-07 14:15:54.015: E/AndroidRuntime(10749): at dalvik.system.NativeStart.main(Native Method) 05-07 14:15:54.015: E/AndroidRuntime(10749): Caused by: java.lang.reflect.InvocationTargetException 05-07 14:15:54.015: E/AndroidRuntime(10749): at java.lang.reflect.Method.invokeNative(Native Method) 05-07 14:15:54.015: E/AndroidRuntime(10749): at java.lang.reflect.Method.invoke(Method.java:525) 05-07 14:15:54.015: E/AndroidRuntime(10749): at android.view.View$1.onClick(View.java:3833) 05-07 14:15:54.015: E/AndroidRuntime(10749): ... 11 more 05-07 14:15:54.015: E/AndroidRuntime(10749): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8 05-07 14:15:54.015: E/AndroidRuntime(10749): at com.roc.quickynumbers.GameActivity.generateRandomNumbersRbra(GameActivity.java:73) 05-07 14:15:54.015: E/AndroidRuntime(10749): at com.roc.quickynumbers.GameActivity.startGame(GameActivity.java:46) 05-07 14:15:54.015: E/AndroidRuntime(10749): ... 14 more

Thanks in advance!

Edit:

Thanks everyone. I got it working now!

Solution:

int rnd;
    Random rand = new Random();
    ArrayList<Integer> numbersRbra = new ArrayList<Integer>();

    Button[] buttonListRbra = new Button[9];
    buttonListRbra[0] = (Button)findViewById(R.id.tap1);
    buttonListRbra[1] = (Button)findViewById(R.id.tap2);
    buttonListRbra[2] = (Button)findViewById(R.id.tap3);
    buttonListRbra[3] = (Button)findViewById(R.id.tap4);
    buttonListRbra[4] = (Button)findViewById(R.id.tap5);
    buttonListRbra[5] = (Button)findViewById(R.id.tap6);
    buttonListRbra[6] = (Button)findViewById(R.id.tap7);
    buttonListRbra[7] = (Button)findViewById(R.id.tap8);
    buttonListRbra[8] = (Button)findViewById(R.id.tap9);

    for (int i=0; i < 9; i++) // Loop through your entire list to access all your 9 Buttons
    {
        rnd = rand.nextInt(9) + 1; // Return an int in the range [1,9] => 9 elements

        if(i==0)
        {
           numbersRbra.add(rnd);
           buttonListRbra[i].setText(numbersRbra.get(i).toString()); //Set the Button Text for the first iteration too
        }
        else
        {
          while(numbersRbra.contains(rnd)) // Transformation between Integer and int done automatically
          {
              rnd = rand.nextInt(9) + 1; // Return an int in the range [1,9]
          }
          numbersRbra.add(rnd);
          buttonListRbra[i].setText(numbersRbra.get(i).toString()); // Set it after finding the correct number
        }
      }
Was it helpful?

Solution

You don't understand correctly the Array.

When you create an array like that : new Button[8];.

It can contain 8 element. The allocated array will start at index 0 and stop at index 7. So if you want to store 9 elements like you do, do something like that:

Button[] buttonListRbra = new Button[9];

And loop correctly through it.

The logcat confirm this error. You have an ArrayIndexOutOfBoundsException. You are trying to access an index wich is bigger than the size of the array. Check this to detect the errors from the LogCat yourself.

Edit:

Here is the corrected code that i think you want:

      int rnd;
      Random rand = new Random();
      ArrayList<Integer> numbersRbra = new ArrayList<Integer>();

      Button[] buttonListRbra = new Button[9];
      buttonListRbra[0] = (Button)findViewById(R.id.tap1);
      buttonListRbra[1] = (Button)findViewById(R.id.tap2);
      buttonListRbra[2] = (Button)findViewById(R.id.tap3);
      buttonListRbra[3] = (Button)findViewById(R.id.tap4);
      buttonListRbra[4] = (Button)findViewById(R.id.tap5);
      buttonListRbra[5] = (Button)findViewById(R.id.tap6);
      buttonListRbra[6] = (Button)findViewById(R.id.tap7);
      buttonListRbra[7] = (Button)findViewById(R.id.tap8);
      buttonListRbra[8] = (Button)findViewById(R.id.tap9);

      for (int i=0; i < 9; i++) // Loop through your entire list to access all your 9 Buttons
      {
          rnd = rand.nextInt(9) + 1; // Return an int in the range [1,9] => 9 elements

          if(i==0)
          {
              numbersRbra.add(rnd);
              buttonListRbra[i].setText(numbersRbra.get(i)); // Set the Button Text for the first iteration too
          }
          else
          {
              while(numbersRbra.contains(rnd)) // Transformation between Integer and int done automatically
              {
                  rnd = rand.nextInt(9) + 1; // Return an int in the range [1,9]
              }
              numbersRbra.add(rnd);
              buttonListRbra[i].setText(numbersRbra.get(i)); // Set it after finding the correct number
          }
      }

I strongly recommend that you understand the code first before using it and learn a bit about how to use the ArrayList. As i have already said in my first answer, you should try to understand the error the LogCat provide you. It can only helps you in your future applications.

OTHER TIPS

 Button[] buttonListRbra = new Button[8];
  buttonListRbra[0] = (Button)findViewById(R.id.tap1);
  buttonListRbra[1] = (Button)findViewById(R.id.tap2);
  buttonListRbra[2] = (Button)findViewById(R.id.tap3);
  buttonListRbra[3] = (Button)findViewById(R.id.tap4);
  buttonListRbra[4] = (Button)findViewById(R.id.tap5);
  buttonListRbra[5] = (Button)findViewById(R.id.tap6);
  buttonListRbra[6] = (Button)findViewById(R.id.tap7);
  buttonListRbra[7] = (Button)findViewById(R.id.tap8);
  buttonListRbra[8] = (Button)findViewById(R.id.tap9);

You are creating an array that can hold 8 button references from index 0 to 7, and after that you are trying to access the button at 8 index, but index 8 does not exist i.e it is out of bounds from the array's perspective, so you are getting exception while trying to execute buttonListRbra[8].

You are adding an int (primitive data type) into a List of Integer (Hullclass). Try adding Integer.valueof(randomPick[i]). Next time, please include Logcat output in your post, so we can read the stacktrace. Also, I think you dont need the Array you are using. Just insert the values into the List immediately.

Edit: Above posts are right, you are also creating an ArrayIndexOutOfBoundsException by accessing field 9 of an Array which is only 8 fields long. You should consider reading the error log to find such errors by yourself the next time.

Edit: You are basically doing

randomPick[i] = rnd; numbersRbra.add(randomPick[i]);

but instead you can just

numbersRbra.add(rnd);

You don't youse the randomPick[] somewhere else and hence are filling an Array and a List with the same random values for no particular reason (I guess).

If you then want to set the Label of the Button to the generated random number, you simply call buttonListRbra[i].setText(numbersRbra.get(i));.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top