0

I'm having a problem creating an application that can interact with firebase and get information stored in the server. But I am facing some problem here at line 53. Please be informative as I don't know why this is happening. When I tried with offline direct "Questions", it worked completely fine. But when connecting the server, it fails every time.

//Line 53
questions = list.get(index);

This is what I did in the QuestionAnsActivity.java

...
import static com.company.appname.QuizSplash.list;
...

public class QuestionAnsActivity extends AppCompatActivity {
...
    List<Questions> allQuestionsList;
    Questions questions;
    int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question_ans_activity);
       ...


        allQuestionsList = list;
        Collections.shuffle(allQuestionsList);

       // The below code is my line 53
        questions = list.get(index);
}}

This is what I did in QuizSplash.java

public class QuizSplash extends AppCompatActivity {


    public static ArrayList<Questions> list;

    DatabaseReference databaseReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz_splash);

        list = new ArrayList<>();
        databaseReference = FirebaseDatabase.getInstance().getReference("Question");

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                    Questions questions = dataSnapshot.getValue(Questions.class);
                    list.add(questions);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

        Intent intent = new Intent(QuizSplash.this, QuestionAnsActivity.class);
        startActivity(intent);

    }

}

This is in Questions.java

public class Questions {

    String question;
    String option1;
    String option2;
    String option3;
    String option4;
    String answer;

//==================================================================================================
//                                      Empty constructor
//==================================================================================================
    public Questions() {
    }


//==================================================================================================
//                                       Constructor
//==================================================================================================
    public Questions(String question, String option1, String option2,
                     String option3, String option4, String answer) {
        this.question = question;
        this.option1 = option1;
        this.option2 = option2;
        this.option3 = option3;
        this.option4 = option4;
        this.answer = answer;
    }

//==================================================================================================
                            // Getter and setter methods start here
//==================================================================================================
    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getOption1() {
        return option1;
    }

    public void setOption1(String option1) {
        this.option1 = option1;
    }

    public String getOption2() {
        return option2;
    }

    public void setOption2(String option2) {
        this.option2 = option2;
    }

    public String getOption3() {
        return option3;
    }

    public void setOption3(String option3) {
        this.option3 = option3;
    }

    public String getOption4() {
        return option4;
    }

    public void setOption4(String option4) {
        this.option4 = option4;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Rafi
  • 39
  • 5
  • The problem happens before line 53. The out of bounds exception means that the list does not contain that index. Since you are querying the first index (0), that means you have an empty list. And since you would get a different error if the list wasn't instantiated, I would guess that the problem is coming from your overrided onDataChange method – dbrewster Oct 27 '21 at 18:27
  • Someone familiar with using firebase may be able to point out your problem offhand, but most likely you will need to add logging to help identify the problem. – dbrewster Oct 27 '21 at 18:31

1 Answers1

0

As I see, you have two classes. The QuizSplash class where you actually perform the database call to get the questions, and the QuestionAnsActivity class, where you assign the result that is coming from the database to an existing list called allQuestionsList. But that's not how you should deal with the Realtime Database.

Don't forget that data is loaded from the Realtime Database asynchronous, meaning that any code that needs data from the database, needs to be inside the onDataChange() method, or be called from there. So you cannot set the list object public and static and simply use it in another activity. You need to wait for the data to complete loading. So you either use that list inside the callback, or you can use my answer from the following post:

Where I have explained how you can propagate the result using an interface. If you understand Kotlin, you might also be interested in reading the following article:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Can you please tell me where should I specifically change the code. Cause I'm not good in this type of thing at all. May be you should show me the code and where I can use that. – Rafi Oct 28 '21 at 07:36
  • Rafi, you should make your own attempt given the information in the answer and ask another question if something else comes up. Remember, we are willing to further assist you when you'll post another question, with another issue. – Alex Mamo Oct 28 '21 at 07:43
  • You're saying that I've to wait for the data to load. But can I use a loading page with duration for about 3 seconds, will that work? – Rafi Oct 28 '21 at 07:46
  • Yes, indeed you need to wait for the data. If that's what you need, then you can use a page and indicate a progress bar, while getting the data from the database. That's a common practice. – Alex Mamo Oct 28 '21 at 07:54