Solved ArraList clearing it self alone

Discussion in 'Spigot Plugin Development' started by joschma, Feb 22, 2021.

  1. Hello I have a problem with my plugin.
    When I try this code :

    A regular file :
    Code (Java):
    Arena a = [an object]
    Player p = [a regular player]
    ArrayList<Player> hiders = a.getHiders();
    p.sendMessage("before " + a.getForgottenHiders());
    p.sendMessage("after " + a.getForgottenHiders());
    Arena :
    Code (Java):
    ArrayList<Player> hiders = new ArrayList<Player>();
    ArrayList<Player> forgottenHiders = new ArrayList<Player>();
    public ArrayList<Player> getHiders () {
    return hiders;
    public ArrayList<Player> getForgottenHiders () {
    return forgottenHiders;
    I get this :
    I don't know why it is doing this. I am not modifing at all (no where) the ForgottenHiders ArrayList
    Can you help me pleas.
    Thanks !
    #1 joschma, Feb 22, 2021
    Last edited: Feb 22, 2021
  2. Well, you ARE modifying it. hiders.remove(p);
  3. Not at all, look at it closly.
    I am modifing the list : hiders wich comes form the list a.getHiders()
    But the problem is that the list ForgottenHiders is clearing it self and I don't know why. I don't want the list ForgottenHiders to modify it self.
    If you want I can past all my code on a web site but it's pretty long and nothing change until this moment.
    #3 joschma, Feb 22, 2021
    Last edited: Feb 22, 2021
  4. Post the full Arena class, please
  5. #5 joschma, Feb 22, 2021
    Last edited: Feb 22, 2021
  6. Strahan


    Check any other part of the plugin that modifies the list, debug it. If you remove hiders.remove(p) does the output stay as it is?

    Lastly you should be declaring those as List = ArrayList not ArrayList = ArrayList per SOLID design principles. Also you are spelling seeker wrong.
  7. I replaced all my ArrayList with List, the problem still persists !

    Now that I removed hiders.remove(p); my player is staying in both Lists. It's weard...

    Code (Text):
    public Spectator(Arena a, Player p) {
            p.sendMessage("constructor " + a.getForgottenHiders());
            setSpec(a, p);
    public void setSpec(Arena a, Player p) {
           List<Player> hiders = a.getHiders();
           p.sendMessage("before forgotten " + a.getForgottenHiders());
           p.sendMessage("before hiders " + hiders);
           p.sendMessage("after forgotten " + a.getForgottenHiders());
           p.sendMessage("after hiders " + hiders);
           p.sendMessage("spec " + a.getForgottenHiders());
    #7 joschma, Feb 22, 2021
    Last edited: Feb 22, 2021
  8. Could you like upload your source code to github or something?
  9. In your Arena class, you initialize those two ArrayLists and in your constructor you re-assign them to the arguments. Why? Choose one.
    Also show us what you are passing to the constructor.

    Also, package names are uppercase.
    No one said that would fix it, it's just better design.
  10. Here is all my code
    The file that we are talking about are under :
    A regular file = GuessWho/src/fr/joschma/GuessWho/Object/
    Arena = GuessWho/src/fr/joschma/GuessWho/Object/
  11. I don't understand you. I am not native english so it's a bit hard to understand.
  12. #13 joschma, Feb 22, 2021
    Last edited: Feb 22, 2021