5

My problem is that I keep getting the error in the title during debugging.

array[i] = java.lang.IndexOutOfBoundsException : Invalid array range 10 to 10

The following is the for loop. I entered values people = 10, payer = 4, rest = 6.

int[] array = new int[people];
Random rand = new Random();
int rest = people-payer;

for(int i=0; i<people; i++) {
    if(payer<=0) { /*Payers are already decided*/
        array[i] = 0;
        continue;
    }
    if(rest<=0) {
        array[i] = 1;
        continue;
    }
    else {
        int randomNumber = rand.nextInt(2);
        array[i] = randomNumber;
        if (randomNumber == 1)
            payer--;
        if (randomNumber == 0)
            rest--;
    }
}

I think the error is saying that I'm trying to access an index 10 of my array which doesn't exist. I don't know where I am accessing array[10]. Or maybe the problem is somewhere else??

Thank you for your time :)

+addition

This is the entire code but it's long so.. i don't know if it would help. The for loop is inside the onClick method.

package activities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chloe.myapplication.R;

import java.util.Random;

public class PayActivity extends ActionBarActivity implements View.OnClickListener {
    Button submitButton, flip;
    Switch switchButton;
    EditText et_totalPeople, et_payPeople, et_amount;
    TextView tv_payPeople, tv_people;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_numofpeople);

        submitButton = (Button) findViewById(R.id.submitButton);
        switchButton = (Switch)findViewById(R.id.switchButton);
        et_totalPeople= (EditText) findViewById(R.id.et_totalPeople);
        et_payPeople= (EditText) findViewById(R.id.et_payPeople);
        et_amount = (EditText) findViewById(R.id.et_amount);

        switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked == true) {
                    /*After entering a value in the first editText and pressing enter,
                     * the qwerty keyboard comes up and won't move to third editText even though I push enter...SOLVE */
                    et_payPeople.setEnabled(false);
                    et_payPeople.setClickable(false);
                } else {
                    et_payPeople.setEnabled(true);
                    et_payPeople.setClickable(true);
                }
            }
        });
        et_totalPeople.setSelectAllOnFocus(true);
        et_payPeople.setSelectAllOnFocus(true);
        et_amount.setSelectAllOnFocus(true);
        submitButton.setOnClickListener(this);
    }
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.submitButton:
                int people = Integer.parseInt(et_totalPeople.getText().toString());
                if(people<1 || people>100) {
                    Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
                    break;
                }
                int amount = Integer.parseInt(et_amount.getText().toString());
                if(amount<1 || amount>10000000) {
                    Toast.makeText(getApplicationContext(), "Enter values 1~10,000,000", Toast.LENGTH_LONG).show();
                    break;
                }
                /*Only certain people pay*/
                if(switchButton.isChecked()==false) {
                    int payer = Integer.parseInt(et_payPeople.getText().toString());
                    if(payer<1 || payer>100) {
                        Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
                        break;
                    } /*All three values are valid: SHOW CARDS*/
                    else {
                        int[] array = new int[people];
                        Random rand = new Random();
                        int rest = people-payer;
                        for(int i=0; i<people; ++i) {
                            /*Payers are already decided*/
                            if(payer<=0) {
                                array[i] = 0;
                                continue;
                            }
                            if(rest<=0) {
                                array[i] = 1;
                                continue;
                            }
                            else {
                                int randomNumber = rand.nextInt(2);
                                array[i] = randomNumber;
                                if (randomNumber == 1)
                                    payer--;
                                if (randomNumber == 0)
                                    rest--;
                            }
                        }
                        AlertDialog.Builder dialog = new AlertDialog.Builder(PayActivity.this);
                        dialog.setView(R.layout.card_questionmark);
                        for(int i=0; i<people; i++) {
                            /*PASS*/
                            if(array[i] == 0) {
                                dialog.setView(R.layout.card_questionmark);
                                dialog.setPositiveButton("FLIP", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        new AlertDialog.Builder(PayActivity.this)
                                        .setView(R.layout.card_pass)
                                        .setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                finish(); /*move on to next value in array*/
                                            }
                                        });
                                    }
                                });
                                /*If not the first card, show previous card*/
                                if(i!=0) {
                                    dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
                                       public void onClick(DialogInterface dialog, int which) {
                                           finish();
                                       }
                                    });
                                    i--; /*return to previous value in array*/
                                } /*First card*/
                                else {
                                    dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            Toast.makeText(getApplicationContext(), "No previous card", Toast.LENGTH_LONG).show();
                                        }
                                    });
                                }
                            }/*YOU PAY*/
                            else {
                                dialog.setView(R.layout.card_questionmark);
                                dialog.setPositiveButton("FLIP", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        new AlertDialog.Builder(PayActivity.this)
                                            .setView(R.layout.card_youpay)
                                            .setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog, int which) {
                                                    finish(); /*move on to next value in array*/
                                                }
                                            });
                                    }
                                });
                                /*If not the first card, show previous card*/
                                if(i!=0) {
                                    dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            finish();
                                        }
                                    });
                                    i--; /*return to previous value in array*/
                                } /*First card*/
                                else {
                                    dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            Toast.makeText(getApplicationContext(), "No previous card", Toast.LENGTH_LONG).show();
                                        }
                                    });
                                }
                            }
                        }
                    }
                } /*Everyone Pays*/
                else {

                }
            break;
        }
    }
}
awefawe
  • 75
  • 1
  • 6
  • Can you post the section of your code where you declared the arrays? – UnknownOctopus Jun 29 '15 at 22:11
  • sry forgot that part - added it! – awefawe Jun 29 '15 at 22:11
  • 3
    Do you get this exception *only* during debugging? The code you've shown appears to be working just fine for me. – Marvin Jun 29 '15 at 22:20
  • 1
    Do you have the line number in the logs? – hoomi Jun 29 '15 at 22:20
  • Show where you're assigning `people` and `payer` variables. – hungryghost Jun 29 '15 at 22:24
  • @marvin I'm looking at the 'variables' window. I'm not sure I understand what you mean by 'only during debugging' – awefawe Jun 29 '15 at 22:28
  • @hoomi i don't think so.. – awefawe Jun 29 '15 at 22:28
  • Does your logcat specifically say the error is at the `array[i] = 0;` line? – hungryghost Jun 29 '15 at 22:32
  • @awefawe: I mean: If you only *run* the code, without looking at the variables - does it still throw this exception? – Marvin Jun 29 '15 at 22:32
  • @hungryghost that's the last thing it said in the variables window – awefawe Jun 29 '15 at 22:44
  • @marvin um.. without looking at the variables, if i keep debugging, run():1294, ZygoteInit$MethodAndArgsCaller (com.android.internal.os) comes up – awefawe Jun 29 '15 at 22:46
  • Just looking at your code, I don't see anything wrong with that loop. It should work. Did you also get the error at runtime? If runtime works, but it errors only in your debugger/inspector, then it might just be an issue with your environment. Try clearing cache, relaunching your IDE, and rebuilding. – hungryghost Jun 29 '15 at 22:49
  • @hungryghost Does runtime mean when I'm just clicking through the app on my phone? When I do that I get an error popup after pushing the 'submit' button.. T.T – awefawe Jun 29 '15 at 22:52
  • Yeah, by "runtime", I mean running the app on your phone/device/emulator and seeing what error you get there. Once you hit an error/exception, you'll see a stack trace in your logcat window, which indicates the exact line numbers for each exception in the stack. You're usually going to care about the _top-most_ exception/line number related to your specific class. That's what you should focus on. – hungryghost Jun 29 '15 at 22:57
  • clean your code till your pc freezes. guess will help – Elltz Jun 30 '15 at 04:09

1 Answers1

-5

i can tell that if your array have 10 or more space in it , it will not give you this error , becouse this error say that you went out of the space you declare this array will get . and this loop runing only 10 times. try to debug it.

Ori Yampolsky
  • 125
  • 13