I am making a quiz app where i will add questions in firebase database and it will show in my app in text view, but i am getting error :- java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
i have decleared a variable index;
this is my code;
enter code here
package com.example.quizz;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Collections;
public class Main_Area extends AppCompatActivity {
DatabaseReference databaseReference;
ArrayList<Modelclass> questionList;
Modelclass modelclass;
int index = 0;
TextView textView;
Button button2, button3, button4, button5,nxt_btn;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.main_area);
textView = findViewById(R.id.textView);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
button5 = findViewById(R.id.button5);
nxt_btn = findViewById(R.id.button6);
questionList = new ArrayList<>();
databaseReference = FirebaseDatabase.getInstance().getReference("Question");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot: snapshot.getChildren()){
Modelclass modelclass = dataSnapshot.getValue(Modelclass.class);
questionList.add(modelclass);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
// questionList.add(new Modelclass("what is capital of india ", " india", "virat","delhi","mumbai","delhi"));
//questionList.add(new Modelclass("what is capital of us ", " india", "virat","delhi","WD","WD"));
// questionList.add(new Modelclass("what is capital of AUSTRALIA ", "canbra", "virat","delhi","mumbai","canbra"));
// questionList.add(new Modelclass("what is capital of uk ", " india", "london","delhi","mumbai","london"));
// questionList.add(new Modelclass("what is capital of canada ", "montreal", "london","delhi","mumbai","montreal"));
Collections.shuffle(questionList);
modelclass = questionList.get(index);
setALLdata();
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (questionList.get(index).getAnswer().equals(button2.getText().toString())){
button2.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#59c639")));
} else{
button2.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FF0000")));
}
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (questionList.get(index).getAnswer().equals(button3.getText().toString())){
button3.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#59c639")));
} else{
button3.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FF0000")));
}
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (questionList.get(index).getAnswer().equals(button4.getText().toString())){
button4.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#59c639")));
} else{
button4.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FF0000")));
}
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (questionList.get(index).getAnswer().equals(button5.getText().toString())){
button5.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#59c639")));
} else{
button5.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FF0000")));
}
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
}
});
nxt_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
index++;
modelclass= questionList.get(index);
setALLdata();
button2.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#81358E")));
button3.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#81358E")));
button4.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#81358E")));
button5.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#81358E")));
button2.setEnabled(true);
button3.setEnabled(true);
button4.setEnabled(true);
button5.setEnabled(true);
}
});
}
private void setALLdata() {
textView.setText(modelclass.getQuestion());
button2.setText(modelclass.getOption1());
button3.setText(modelclass.getOption2());
button4.setText(modelclass.getOption3());
button5.setText(modelclass.getOption4());
}
}
in this above code, the the that i have added in firebase will add in the list questionList and then from there it will display in my play, but the problem is that index is 0 which means the questions are not able to add in the list, but i have written all the code correct and not able to find any problem.