0

I'm trying to collect all players in the Arraylist with this (This is spigot 1.16.5):

Arraylist constructor: public static ArrayList<Player> playerInGame = new ArrayList<>();

public void makePlayerWin(Player perdente) {
        Iterator<Player> it = playerInGame.iterator();
        if(!it.hasNext()) return;
        Player players = it.next();
        System.out.println(1);
        if(playerInGame.contains(perdente) && players.getWorld() == Bukkit.getWorld(plugin.Game) || playerInGame.contains(perdente) && !perdente.isOnline()) {
            System.out.println(2);
            playerInGame.remove(perdente);
            LobbyJoin(players);
            players.sendTitle(plugin.cc("&6Hai vinto!"), plugin.cc("&7Sei tornato alla lobby!"));
            playerInGame.remove(players);
        }
    }

But I get this error:

Error in console

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
  • 1
    [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Joachim Sauer Jan 21 '21 at 15:03
  • Arraylist is not threadsafe. You can make the arraylist to synronized list with Collections.syncronizedList() – olgunyldz Jan 21 '21 at 15:04
  • @OlgunYILDIZ like this? Iterator it = Collections.synchronizedList(playerInGame).iterator(); – Mike Chilled Jan 21 '21 at 15:18
  • Synchronizing the array list won't solve the concurrent modification exception, because the problem is hanging on to an open iterator while the list is being modified. Synchronization over individual calls to the list won't fix this! – Joachim Sauer Jan 21 '21 at 15:26
  • @JoachimSauer and, with this, what can i do? – Mike Chilled Jan 21 '21 at 15:30
  • The code for `onQuit` isn't shown. I suspect that method marches through the list, while the method `makePlayerWin` also executes. If they execute on different threads, the solution would be using a lock while iterating, in both methods. If, on the other hand, `onQuit` calls `makePlayerWin`, you'd need another solution. – Mark Jeronimus Jan 21 '21 at 16:22

0 Answers0