-1

OUT OF MEMORY ERROR ON THE LINE THAT ADDS THE OBJECT TO THE ArrayList - LAST LINE

I want every time I click the button saveScore, the name of the first team selected from the first spinner, the name of the second team selected from the second spinner, the score from the first editText and the score from the second editText to be the arguments of my class object.

teamOneScore & teamTwoScore are two EditText where I enter the score for each team selected from the spinners and arrList is my ArrayList of objects .

I have a class - TeamsViewModel with 4 members of type String: team1Name, team2Name, team1Score, team2Score.

 saveScore.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
        boolean resultAddTeamOneScore = addTeamOneScore(weakActivity, teamOneScore.getText().toString().trim(), teamTwoScore.getText().toString().trim());
        boolean resultAddTeamTwoScore = addTeamTwoScore(weakActivity, teamTwoScore.getText().toString().trim(),teamOneScore.getText().toString().trim());
      if (resultAddTeamOneScore && resultAddTeamTwoScore) {
          dialog.dismiss();
          }

     /** save the teams selected from the spinner */
     String selectedT1 = teamOneSpinnerSelected(teamOneSpinner);
     String selectedT2 = teamTwoSpinnerSelected(teamTwoSpinner);
     while (!selectedT1.equals(selectedT) {
        /** add to the ArrayLists of objects the teams selected and their's score*/
        arrTeam.add(new TeamsViewModel(selectedT1, teamOneScore.getText().toString().trim(), selectedT2, teamTwoScore.getText().toString().trim()));
                    }
                }
            });

1 Answers1

0

Change

while (selectedT1 != selectedT2) 

to

while (! selectedT1.equals(selectedT2))

The condition (selectedT1 != selectedT2) may be always true, although the two Strings have identical content, and you have an endless loop leading to the OutOfMemoryError. In Java Strings must not be compared using != or ==, always use equals().