lundi 23 septembre 2019

array/list not null in java java.lang.IndexOutOfBoundsException: Index: 2, Size: 2

I have a method in which array is returned and it has two values and can be long up to index 3 i.e. 0,1,2. I have written if statement which checks if the array which is returned is empty/null or not. But I get an error i.e. Caused by: java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 I can't call method .size() of .isEmpty(), eclipse doesn't recognize these method on my Array or ArrayList.

A ArrayList of TopThreePlayers is returned which includes Playername and Goal scores, sometimes it can have nothing depending on the textfile it is querying, so when I check if the TopThreePlayers is null it gives me error.

 public static void outputWriter_topthreePlayers(String method, String argument, TopThreePlayers topThreeUsers) {

try {

    if (topThreePlayers!=null ){
    Writer writer = new FileWriter("output.txt",true);
    for (int i =0; i<3; i++)
    writer.write("TOPTHREEUSERS for Song" + argument1 + " are Users  " + topThreeUsers.topThreePlayers[i].name + " times played " +topThreeUsers.topThreePlayers[i].goalscore+" By Player "\n" );
    writer.close();
    }
}

Another Class, which calls this

else if (method.equals("getTopPlayersGoalScored")) {
  TopThreeUsers result=player.getTopThreePlayerByGoal(argument);
    OutputFile.outputWriter_topthreeusers(method, argument, result );
            }

getTopPlayersGoalScored Method does something like

ArrayList <PlayerCounterImpl> userList = new ArrayList<PlayerCounterImpl>();

with This logic

playerCounter = new PlayerCounterImpl(part2,part3);
                userList.add(playerCounter);
             }
        }
        sc.close();
        Collections.sort(playerList, new Sorting());
        PlayerCounterImpl[] topThree = {playerList.get(0),playerList.get(1),playerList.get(2)};
        TopThreeUsersImpl result = new TopThreePlayersImpl(topThree);
        return result;

This method has the return type TopThreePlayers which is

 public class TopThreePlayersImpl extends TopThreePlayers {

private static final long serialVersionUID = 1L;

public TopThreePlayersImpl() {

}
public TopThreePlayersImpl(Games.PlayerCounter topThreePlayers[]) {
    this.topThreePlayers=topThreePlayers;

}

}

Aucun commentaire:

Enregistrer un commentaire