Fixing java.lang.IndexOutOfBoundsException after Game Ends

Insert here your troubles, infos and request about YSoccer's technical questions.
Post Reply
ali.abdelaziz
Someone..
Posts: 2
Joined: Wed Feb 21, 2024 11:12 pm

Fixing java.lang.IndexOutOfBoundsException after Game Ends

Post by ali.abdelaziz »

Every time I play a match, the statistics screen remains visible for an extended time post-match, and then the game experiences a crash with error:

Code: Select all

java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 0
so I have updated the
com.ygames.ysoccer.competitions.Compitiation.getTeam

Code: Select all

public Team getTeam(int side) {
    	int teamIndex = getTeamIndex(side);
    	
    	// Added the following checks to Fix
    	// java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 0
    	if(teams == null || teams.size() <= teamIndex || teamIndex < 0) {    		
    		Gdx.app.error("Team", "No valide team index found; Team not found <teamIndex=" + teamIndex + ", teams.size =" + teams.size() + ">");
    		return null;
    	}
    	// End Checks
    	
        return teams.get(teamIndex);
    }
The following outlines the stack trace resulting from the crash.

Code: Select all

$ gradlew desktop:run

> Task :desktop:run
[Controllers] added manager for application, 1 managers active
java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 0
        at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100)
        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
        at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
        at java.base/java.util.Objects.checkIndex(Objects.java:385)
        at java.base/java.util.ArrayList.get(ArrayList.java:427)
        at com.ygames.ysoccer.competitions.Competition.getTeam(Competition.java:188)
        at com.ygames.ysoccer.competitions.Competition.dropSuspensions(Competition.java:475)
        at com.ygames.ysoccer.competitions.Competition.matchCompleted(Competition.java:425)
        at com.ygames.ysoccer.screens.MatchScreen.quit(MatchScreen.java:115)
        at com.ygames.ysoccer.screens.MatchScreen.access$000(MatchScreen.java:20)
        at com.ygames.ysoccer.screens.MatchScreen$1.quitMatch(MatchScreen.java:43)
        at com.ygames.ysoccer.match.Match.quit(Match.java:615)
        at com.ygames.ysoccer.match.MatchState.quitMatch(MatchState.java:88)
        at com.ygames.ysoccer.match.MatchStateEnd.checkConditions(MatchStateEnd.java:67)
        at com.ygames.ysoccer.match.SceneFsm.think(SceneFsm.java:127)
        at com.ygames.ysoccer.match.Scene.update(Scene.java:24)
        at com.ygames.ysoccer.match.Match.update(Match.java:40)
        at com.ygames.ysoccer.screens.MatchScreen.render(MatchScreen.java:67)
        at com.ygames.ysoccer.framework.GLGame.render(GLGame.java:146)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:232)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:127)
The following outline the logging output after implementation the Fix, and playing two games:
$ gradlew desktop:run

> Task : desktop:run
[Controllers] added manager for application, 1 managers active
[Team] No valide team index found; Team not found <teamIndex=-1, teams.size =0>
[Team] No valide team index found; Team not found <teamIndex=-1, teams.size =0>
[Team] No valide team index found; Team not found <teamIndex=-1, teams.size =0>
[Team] No valide team index found; Team not found <teamIndex=-1, teams.size =0>
[Controllers] removed manager for application, 0 managers active
Post Reply