-1

i am making a quiz app and got stuck on the following error my logcat is given following

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.example.owner.quiz, PID: 25307
                                                                    java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
                                                                        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                        at java.util.ArrayList.get(ArrayList.java:308)
                                                                        at com.example.owner.quiz.MainActivity$1.onClick(MainActivity.java:50)
                                                                        at android.view.View.performClick(View.java:4791)
                                                                        at android.view.View$PerformClick.run(View.java:19884)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5268)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

when i click on the (MainActivity.java:50) it will go to the main activity and showing currentQ=quesList.get(qid); this following is my main activity code here

public class MainActivity extends Activity {
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DbHelper db=new DbHelper(this);
    quesList=db.getAllQuestions();
    currentQ=quesList.get(qid);
    txtQuestion=(TextView)findViewById(R.id.textView1);
    rda=(RadioButton)findViewById(R.id.radio0);
    rdb=(RadioButton)findViewById(R.id.radio1);
    rdc=(RadioButton)findViewById(R.id.radio2);
    butNext=(Button)findViewById(R.id.button1);



    setQuestionView();


    butNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
            if(currentQ.getANSWER().equals(answer.getText()))
            {
                score ++ ;
            }
            currentQ=quesList.get(qid);
            setQuestionView();
        }
    });
}

can anyone tell me how to deal with this

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Your error says it all. Size is 4 so `quesList.get(4)` will throw exception. – Nongthonbam Tonthoi Mar 18 '17 at 13:36
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Nongthonbam Tonthoi Mar 18 '17 at 13:37

3 Answers3

1

You're changing the value of qid to 4 somewhere that's not in your included code, but your quesList is only 4 items long, so the valid indexes for it are 0-3.

zsmb13
  • 85,752
  • 11
  • 221
  • 226
-1

you try verify using if, see exemple

if (qid < quesList.size()) {
  currentQ=quesList.get(qid);
}
Thiago Neves
  • 91
  • 1
  • 7
-1

this may be caused by the fact that an sqlite index starts at 1 (and not at 0 like List), can you show your DbHelper class code? specifically the getAllQuestions() method.

nir
  • 302
  • 3
  • 12
  • public List getAllQuestions() { List quesList = new ArrayList(); String selectQuery = "SELECT * FROM " + TABLE_QUEST; dbase=this.getReadableDatabase(); Cursor cursor = dbase.rawQuery(selectQuery, null); –  Mar 18 '17 at 13:48
  • if (cursor.moveToFirst()) { do { Question quest = new Question(); quest.setID(cursor.getInt(0)); quest.setQUESTION(cursor.getString(1)); quest.setANSWER(cursor.getString(2)); quest.setOPTA(cursor.getString(3)); quest.setOPTB(cursor.getString(4)); quest.setOPTC(cursor.getString(5)); quesList.add(quest); } while (cursor.moveToNext()); } –  Mar 18 '17 at 13:48
  • OK, this looks ok, you get a list of "Question" with a certain size back from this method. pay attention that you set the Question's ID as the 0 position in the cursor. i assum this is your auto increment ID, corect me if im wrong, this auto increment starts with the value of 1. now i need to see if you are using this ID to get the question index from the qustion List in your MainActivity. show me the codewhere you change the value of the "qid" veriable in the main activity. – nir Mar 18 '17 at 14:09