I was Working with ArrayList using class where i have to put its value in gridlayouts options. First Time on OnCreate It worked fine and setText to correct place But after calling the class SECOND TIME It crashed. HOW CAN SAME CODE WORK ONCE AND CRASH NEXT TIME (I think error is at last lines:-> See From back classes)

public class playGame extends Fragment {
    View view;
    Button option1Button,option2Button,option3Button,option4Button;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle bundle) {
        view=inflater.inflate(R.layout.fragment_play_game, container, false);
        option1Button=view.findViewById(R.id.option1);
        option2Button=view.findViewById(R.id.option2);
        option3Button=view.findViewById(R.id.option3);
        option4Button=view.findViewById(R.id.option4);
        String gameName=getArguments().getString("Name");
        TextView levelNameTextView=view.findViewById(R.id.levelNameTextView);
        levelNameTextView.setText(gameName);
        Button playAgainButton=view.findViewById(R.id.gameOverTry);
        playAgainButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(view).navigate(R.id.action_playGame_to_gameOver);
            }
        });
        final ProcessGame processGame=new ProcessGame();
        GetAndPutData();
        option1Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                processGame.compareWithCorrect(option1Button.getText().toString());
            }
        });
        option2Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                processGame.compareWithCorrect(option2Button.getText().toString());
            }
        });
        option3Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                processGame.compareWithCorrect(option3Button.getText().toString());
            }
        });
        option4Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                processGame.compareWithCorrect(option4Button.getText().toString());
            }
        });
        return view;
    }
    public class ProcessGame{
        String question,option1,option2,option3,option4,correctAnswer;
        TextView questionBox=view.findViewById(R.id.questionBox);
        List<String> questionlist=new ArrayList<String>();
        public void setValues(String question,String option1,String option2,String option3,String option4,String correctAnswer){
            this.option1=option1;
            this.option2=option2;
            this.option3=option3;
            this.option4=option4;
            this.question=question;
            this.correctAnswer=correctAnswer;
            questionlist.add(0,option1);
            questionlist.add(1,option2);
            questionlist.add(2,option3);
            questionlist.add(3,option4);
            shuffleOption();
            questionBox.setText(question);
        }
        public void shuffleOption(){
            Collections.shuffle(questionlist);
            putOptionsInButton();

        }
        public void putOptionsInButton(){
            option1Button.setText(questionlist.get(0));
            option2Button.setText(questionlist.get(1));
            option3Button.setText(questionlist.get(2));
            option4Button.setText(questionlist.get(3));
        }
        public void compareWithCorrect(String pressedButtonValue){
            TextView feedbackBox=view.findViewById(R.id.feedbackBox);
            if (pressedButtonValue==correctAnswer){
                feedbackBox.setText("Correct :-)");
                GetAndPutData();
            }
            else{
                feedbackBox.setText("Wrong :-(");
                shuffleOption();
            }
        }
    }
    public void GetAndPutData(){
        ProcessGame processGame=new ProcessGame();
        processGame.setValues("1+1=?","2","1","3","4","2");
    }
}

Here is my error:->

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.get(ArrayList.java:411)
        at com.example.quiz_createandplay.playGame$ProcessGame.putOptionsInButton(playGame.java:117)
        at com.example.quiz_createandplay.playGame$ProcessGame.shuffleOption(playGame.java:113)

1 Answers

0
Ezaldeen sahb On Best Solutions

when the answer is right you are calling getAndPutData() which call set values and that's when that app is working fine.but, when the answer is wrong you are sending it to the shuffleOption() which call putOptionsInButton() that see a null list, you have to set DATA to your list even if the answer is wrong for example if you called setValues() inside shuffleOption() you won't have a crash.

    public void shuffleOption(){
        Collections.shuffle(questionlist);
        processGame.setValues("1+1=?","2","1","3","4","2");
        putOptionsInButton();

    }