Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.9.1a is out! (05/10/19); Blog post: Personal Contacts (08/13/20)

Author Topic: IntelManager.getIntel(Class c) returns non-iterable lists  (Read 16 times)

Jaghaimo

  • Ensign
  • *
  • Posts: 11
    • View Profile

Whenever I try to iterate over that list, I get:
Code
19433 [Thread-4] ERROR com.fs.starfarer.combat.CombatMain  - java.util.ConcurrentModificationExceptionjava.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at stellics.StellicsModPlugin.removeStaleIntel(StellicsModPlugin.java:55)
...
Lists should be iterable as they implement Iterable interface. Code to reproduce (called from onGameLoad):
Code
    private void removeDeprecatedIntel(Class<?> c) {
        IntelManagerAPI manager = Global.getSector().getIntelManager();

        for (IntelInfoPlugin i : manager.getIntel(c)) {// line 55
            manager.removeIntel(i);
        }
    }
If I use "low-tech" approach all is fine and well:
Code
    private void removeDeprecatedIntel(Class<?> c) {
        IntelManagerAPI manager = Global.getSector().getIntelManager();
        List<IntelInfoPlugin> intels = manager.getIntel(c);

        for (int i = 0; i < intels.size(); i++) {
            IntelInfoPlugin intel = intels.get(i);
            manager.removeIntel(intel);
        }
    }
Logged

Jaghaimo

  • Ensign
  • *
  • Posts: 11
    • View Profile
Re: IntelManager.getIntel(Class c) returns non-iterable lists
« Reply #1 on: Today at 03:00:13 AM »

User error. Was modifying list that I was iterating over. Should make a copy first...
Code
    private void removeDeprecatedIntel(Class<?> c) {
        IntelManagerAPI manager = Global.getSector().getIntelManager();
        List<IntelInfoPlugin> intels = new ArrayList<IntelInfoPlugin>(manager.getIntel(c));

        for (IntelInfoPlugin i : intels) {
            manager.removeIntel(i);
        }
    }

Leaving OP for Alex's morning chuckle. And sorry for wrong section, just noticed there's modded one too.
« Last Edit: Today at 03:03:47 AM by Jaghaimo »
Logged