I making Quiz App using Firebase Realtime databse when I fetch data and show in cardView nothing happend

Viewed 29
Here is DatabaseClass     
        package com.example.quizapp;
       
        public    class DatabaseClass {
        
            String Question;
            String QA;
            String QB;
            String QC;
            String QD;
            String ans;
        
            public DatabaseClass() {
        
            }
        
            public  DatabaseClass(String question, String QA, String QB, String QC, String QD, String ans) {
                Question = question;
                this.QA = QA;
                this.QB = QB;
                this.QC = QC;
                this.QD = QD;
                this.ans = ans;
        
            }
        
            public String getQuestion() {
                return Question;
            }
        
            public void setQuestion(String question) {
                Question = question;
            }
        
            public String getQA() {
                return QA;
            }
        
            public void setQA(String QA) {
                this.QA = QA;
            }
        
            public String getQB() {
                return QB;
            }
        
            public void setQB(String QB) {
                this.QB = QB;
            }
        
            public String getQC() {
                return QC;
            }
        
            public void setQC(String QC) {
                this.QC = QC;
            }
        
            public String getQD() {
                return QD;
            }
        
            public void setQD(String QD) {
                this.QD = QD;
            }
        
            public String getAns() {
                return ans;
            }
        
            public void setAns(String ans) {
                this.ans = ans;
            }
        }

} here is the main error Here I call the database class and // Error is java.lang.nullpointerexception: attempt to invoke virtual method 'java.lang.object java.util.arraylist.get(int)' on a null object reference

        In QuizQuestionActivity I write only the logic part 
        
   

 package com.example.quizapp;
    
    import static android.content.ContentValues.TAG;
    import static com.example.quizapp.GeoActivity.list;
    
    import android.app.Dialog;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.CountDownTimer;
    import android.util.Log;
    import android.view.View;
    import android.view.WindowManager;
    import android.widget.LinearLayout;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.cardview.widget.CardView;
    
    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 java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class QuizQuestionActivity extends AppCompatActivity {
    
        ProgressBar progressBar;
        CountDownTimer countDownTimer;
        int timerValue = 20;
        DatabaseClass databaseClass;
        List<DatabaseClass> getQuestionList;
        int index = 0;
        TextView card_qustion, optiona, optionb, optionc, optiond;
        CardView cardA, cardB, cardC, cardD;
    
        int correctcount = 0;
        int wrongcount = 0;
        LinearLayout nextbtn;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_quiz_question);
    
            Hook();
            getQuestionList = list;
    
            databaseClass = list.get(index);
    // here is a problem  when I remove line activity is open but data can not be fetched 
    // when I add and click the Geography card app will be crashed. 
    
   
    
    
   
    
            setallData();
    
            cardA.setBackgroundColor(getResources().getColor(R.color.white));
            cardB.setBackgroundColor(getResources().getColor(R.color.white));
            cardC.setBackgroundColor(getResources().getColor(R.color.white));
            cardD.setBackgroundColor(getResources().getColor(R.color.white));
    
            nextbtn.setClickable(false);
            countDownTimer = new CountDownTimer(20000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    timerValue = timerValue - 1;
                    progressBar.setProgress(timerValue);
                }
    
                @Override
                public void onFinish() {
                    Dialogdialog=newDialog(QuizQuestionActivity.this,R.style.Dialog);                      dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
                    dialog.setContentView(R.layout.time_out_dialogbox);
                            dialog.findViewById(R.id.btn_tryagain).setOnClickListener(v -> {
                        Intent intent = new Intent(QuizQuestionActivity.this, HomeActivity.class);
                        startActivity(intent);
                    });
                    dialog.show();
                }
            }.start();
        }
    
        private void getAllData() {
    
            card_qustion.setText(databaseClass.getQuestion());
            optiona.setText(databaseClass.getQA());
            optionb.setText(databaseClass.getQB());
            optionc.setText(databaseClass.getQC());
            optiond.setText(databaseClass.getQD());
            timerValue = 20;
            countDownTimer.cancel();
            countDownTimer.start();
    
        }
    
        private void setallData() {
    
            card_qustion.setText(databaseClass.getQuestion());
            optiona.setText(databaseClass.getAns());
            optionb.setText(databaseClass.getAns());
            optionc.setText(databaseClass.getAns());
            optiond.setText(databaseClass.getAns());
    
            timerValue = 20;
            countDownTimer.cancel();
            countDownTimer.start();
        }
    
        private void Hook() {
    
            getQuestionList = list;
            nextbtn = findViewById(R.id.btnnext);
            progressBar = findViewById(R.id.Quiztimer);
    
            card_qustion = findViewById(R.id.card_question);
            optiona = findViewById(R.id.option1);
            optionb = findViewById(R.id.option2);
            optionc = findViewById(R.id.option3);
            optiond = findViewById(R.id.option4);
    
    
            cardA = findViewById(R.id.cardA);
            cardB = findViewById(R.id.cardB);
            cardC = findViewById(R.id.cardC);
            cardD = findViewById(R.id.cardD);
        }
    
        public void correct(CardView cardView) {
            cardView.setBackgroundColor(getResources().getColor(R.color.teal_700));
            nextbtn.setOnClickListener(v -> {
                correctcount++;
                index++;
                databaseClass = list.get(index);
                resetcolour();
                getAllData();
                setallData();
                enableButton();
            });
        }
    
    
        public void Wrong(CardView cardA) {
            cardA.setBackgroundColor(getResources().getColor(R.color.teal_200));
            nextbtn.setOnClickListener(v -> {
                wrongcount++;
                if (index < list.size() - 1) {
                    index++;
                    databaseClass = list.get(index);
                    resetcolour();
                    setallData();
                    getAllData();
                    enableButton();
                } else {
                    GameWon();
                }
            });
    
        }
    
        private void GameWon() {
    
            Intent intent = new Intent(QuizQuestionActivity.this, ResultActivity.class);
            intent.putExtra("correct", correctcount);
            intent.putExtra("wrong", wrongcount);
            startActivity(intent);
        }
    
    
        public void enableButton() {
            cardA.setClickable(true);
            cardB.setClickable(true);
            cardC.setClickable(true);
            cardD.setClickable(true);
        }
    
        public void disableButton() {
    
            cardA.setClickable(false);
            cardB.setClickable(false);
            cardC.setClickable(false);
            cardD.setClickable(false);
        }
    
        public void resetcolour() {
    
            cardA.setBackgroundColor(getResources().getColor(R.color.white));
            cardB.setBackgroundColor(getResources().getColor(R.color.white));
            cardC.setBackgroundColor(getResources().getColor(R.color.white));
            cardD.setBackgroundColor(getResources().getColor(R.color.white));
    
        }
    
        public void optionaclick(View view) {
            disableButton();
            nextbtn.setClickable(true);
            if (databaseClass.getQA().equals(databaseClass.getAns())) {
                cardA.setBackgroundColor(getResources().getColor(R.color.teal_700));
                if (index < list.size() - 1) {
                    correct(cardA);
                } else {
                    GameWon();
                }
            } else {
                Wrong(cardA);
            }
    
        }
    
        public void optionbclick(View view) {
            disableButton();
            nextbtn.setClickable(true);
            if (databaseClass.getQB().equals(databaseClass.getAns())) {
                cardB.setBackgroundColor(getResources().getColor(R.color.teal_700));
                if (index < list.size() - 1) {
                    correct(cardB);
    
                } else {
                    GameWon();
                }
            } else {
                Wrong(cardB);
            }
    
        }
    
        public void optioncclick(View view) {
            disableButton();
            nextbtn.setClickable(true);
            if (databaseClass.getQC().equals(databaseClass.getAns())) {
                cardC.setBackgroundColor(getResources().getColor(R.color.teal_700));
                if (index < list.size() - 1) {
    
                    correct(cardC);
                } else {
                    GameWon();
                }
            } else {
                Wrong(cardC);
            }
        }
    
        public void optiondclick(View view) {
            disableButton();
            nextbtn.setClickable(true);
            if (databaseClass.getQD().equals(databaseClass.getAns())) {
                cardD.setBackgroundColor(getResources().getColor(R.color.teal_700));
                if (index < list.size() - 1) {
                    correct(cardD);
    
                } else {
                    GameWon();
                }
            } else {
                Wrong(cardD);
            }
   
        }

} // here is Geoactivity is the main activity
// and QuizQuestionActivity is for the logic part and the database class for questions

        and then I create GeoActivity for call data 
        
        package com.example.quizapp
        import static android.util.Log.d;
        import androidx.annotation.NonNull;
        import androidx.appcompat.app.AppCompatActivity;
        import androidx.cardview.widget.CardView
        import android.content.Intent;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.widget.Toast;
        
        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 java.util.ArrayList;
        


        public class GeoActivity extends AppCompatActivity {
           public static  ArrayList<DatabaseClass> list;
            DatabaseReference databaseReference;
            FirebaseDatabase firebaseDatabase;
            CardView geographycard;
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_geo);
                geographycard = findViewById(R.id.geographycard);
        
                list = new ArrayList<>();
        
                databaseReference = FirebaseDatabase.getInstance()
                        .getReference("Question");
                databaseReference.keepSynced(true);
        
        
                databaseReference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
        
                        for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
        
                            DatabaseClass databaseClass = dataSnapshot.getValue(DatabaseClass.class);
                            list.add(databaseClass);
        
                        }
       
                  }
        
                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
        
                        Toast.makeText(GeoActivity.this, "Data can not retrieved", Toast.LENGTH_SHORT).show();
                    
                });
            }

} // here I use CardView or textView to set data

// basically question is that data can not be fetched from the real-time database firebase // and not show in card view //I show data in card view in a simple manner // but I want to change the question and every time when user clicks the next button
// question change every time // and error is java.lang.nullpointerexception: attempt to invoke virtual method 'java.lang.object java.util.arraylist.get(int)' on a null object reference

list null error

Thanks,in advance

0 Answers
Related