Solved ArrayList problem

Discussion in 'Spigot Plugin Development' started by punisher5, Feb 20, 2019.

  1. Hi, I know my problem is more due to Java than spigot, but it's related in my case so I ask my question here.

    I'm trying to stock all the location where the player go to spawn an effect which one stay for ever. So I choosed a for loop but I return an error because I change the arrayList of the locations by adding new location. The loop doesn't like this apparently...

    I saw on google than I have to iterate my array, but after few try I don't get a positive result.

    Anyone has an idea to help me ?


    Code (Java):
    for(Location location : ArianneStringListener.loc) {
               
                location.getWorld().playEffect(location, Effect.FIREWORK_SHOOT, 200);
                }
    Sorry for my english :/
     
  2. I assume you're getting a ConcurrentModificationException?

    See this (avoiding ConcurrentModificationException when removing items)
    See this (adding elements while iterating over a list)
     
  3. Indeed, I'm adding new element so the first link is not really useful I suppose.
    For the second link It don't resolve my problem. I have to get each elements from the list (Location in this case) and spawn an effect in. But I still return an error on it.
     
  4. Show stacktrace/code looking at your current code it can't be a ConcurrentModificationException your are just using the location, you are not modifying the list.
     
  5. you may use

    for(int i = 0;i < ArianneStringListener.loc.size() ; i++)
    Location loc = ArianneStringListener.loc.get(i);

    that would avoid CuncurrentModificationException for sure
    but you should be aware that editing a list while looping like this may cause to use an element twice (or skip it if you remove elements)
     
    #5 emanon, Feb 20, 2019
    Last edited: Feb 20, 2019
  6. Actually I got a NPE on
    Code (Java):
    i.add(location);
    with that code
    Code (Java):
    public class ArianneStringEffect implements Runnable{

        ListIterator<Location> i;
       
        @Override
        public void run() {
           
            if(!UHCState.isState(UHCState.WAIT)) {
            if(ArianneStringListener.loc.isEmpty()) return;
               
            for(Location location : ArianneStringListener.loc) {
                i.add(location);
                i.previous().getWorld().playEffect(i.previous(), Effect.FIREWORK_SHOOT, 200);
                }
            }
           
        }

    }
     
  7. You're getting an NPE because you've not initialized the variable i.
     
  8. o_O oops
     
  9. And now ?

    Code (Java):
    public class ArianneStringEffect implements Runnable{

        ListIterator<Location> i = ArianneStringListener.loc.listIterator();
       
        @Override
        public void run() {
           
            if(!UHCState.isState(UHCState.WAIT)) {
            if(ArianneStringListener.loc.isEmpty()) return;
               
                i.previous().getWorld().playEffect(i.previous(), Effect.FIREWORK_SHOOT, 200);
            }
           
        }

    }
     
  10. I still have a CuncurrentModificationException and I don't know why, who can explain the reason please ? I don't need a spoonfunding, just a java lesson because it seem to be the cause :confused:
     
  11. ListIterator#previous
    What you're doing is moving the iterator back and then moving it back again:
    You need to get the location that is returned when you first do i.previous() and use that for both locations.
     
  12. the question is, are all the locations in the same world if so, you could use take that value from the location variable instead
     
  13. then this should work

    Code (Text):
     for(Location location : ArianneStringListener.loc) {
                i.add(location);
               location.getWorld().playEffect(location, Effect.FIREWORK_SHOOT, 200);
                }
            }
     
     
  14. not this ?
    Code (Java):
    public class ArianneStringEffect implements Runnable{

        ListIterator<Location> i = ArianneStringListener.loc.listIterator();
       
        @Override
        public void run() {
           
            if(!UHCState.isState(UHCState.WAIT)) {
            if(ArianneStringListener.loc.isEmpty()) return;
               
           
            Location location = i.previous();
            location.getWorld().playEffect(location, Effect.FIREWORK_SHOOT, 200);
            }
           
        }

    }
     
  15. that would also work,
     
  16. nop, I just tried it. I still get a CuncurrentModificationException. Let's try yours.
     
  17. Your code return a CuncurrentModificationException too
     
  18. that would mean that it's already in it
     
  19. Ofc you get concurrent exception you are adding value in a list when you are iterating...
     

Share This Page