2016-10-15 40 views
0

this 可能是最接近我的案例,但這也沒有幫助。一個奇怪的ConcurrentModificationException迭代

我在這裏得到ConcurrentModificationException的:

for (Iterator<ProjectileBase> iterator = projectiles.iterator(); iterator.hasNext();) { 
     ProjectileBase proj = iterator.next(); < here 
     if (proj == null || !proj.isValid()) { 
      iterator.remove(); 
      continue; 
     } 
     if (((!proj.ignoreRange()) ? proj.lived <= proj.data.getRange() : true)){ 
      proj.lived++; 
      proj.onTick(); 
     } else { 
      MissileHitEvent event = new MissileHitEvent(proj.shooter, proj.wand, proj, false, null); 
      Bukkit.getPluginManager().callEvent(event); 
      if (!event.isCancelled()) { 
       iterator.remove(); 
       continue; 
      } else { 
       proj.lived = 0; 
      } 
     } 
    } 

雖沒的建議here

是這樣規定的名單:

private List<ProjectileBase> projectiles = new ArrayList<ProjectileBase>(); 

這是對班級建設intialized。有什麼問題?

編輯:控制檯日誌:

[10:01:58] [Craft Scheduler Thread - 3754/WARN]: Exception in thread "Craft Scheduler Thread - 3754" 
[10:01:58] [Craft Scheduler Thread - 3754/WARN]: org.apache.commons.lang.UnhandledException: Plugin MagicWands v1.0 generated an exception while executing task 247 
    at org.bukkit.craftbukkit.v1_9_R2.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:57) 
    at com.destroystokyo.paper.ServerSchedulerReportingWrapper.run(ServerSchedulerReportingWrapper.java:22) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.util.ConcurrentModificationException 
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source) 
    at java.util.ArrayList$Itr.next(Unknown Source) 
    at io.jettymc.DataHolders.ProjectileManager.iterate(ProjectileManager.java:117) 
    at io.jettymc.DataHolders.ProjectileManager$1.run(ProjectileManager.java:232) 
    at org.bukkit.craftbukkit.v1_9_R2.scheduler.CraftTask.run(CraftTask.java:58) 
    at org.bukkit.craftbukkit.v1_9_R2.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53) 
    ... 4 more 

編輯2:嗯,我想這是值得一說,我要建這個項目上Bukkit /插頭(的Minecraft服務器& API)..但我懷疑這是造成這個錯誤的原因?

+0

請顯示您的控制檯日誌。 –

+0

補充說。必須使用pastebin,因爲粘貼錯誤日誌只是不起作用。 – jetp250

+0

錯誤日誌在此處正常工作。爲你更新了你的問題。 – Andreas

回答

1

爲了幫助縮小問題範圍,有一個技巧可能會有所幫助。

假設projectiles是對ArrayList的唯一引用,您可以在迭代過程中臨時將其替換爲不可變列表,因此只有Iterator可以修改列表。如果其他代碼嘗試修改列表,則會發生異常,它會讓您知道它發生的位置,假定您的錯誤處理不會混亂。

實施例:

List<ProjectileBase> projectilesHold = projectiles; 
projectiles = Collections.unmodifiableList(projectiles); 
try { 
    for (Iterator<ProjectileBase> iterator = projectilesHold.iterator(); iterator.hasNext();) { 
     // other code here is unchanged 
    } 
} finally { 
    projectiles = projectilesHold; 
} 

的代碼保存在「保持」變量可修改的列表中,然後包裝列表以使其不可修改的。無論如何,finally塊都將確保可修改列表已恢復。

for循環迭代,然後修改爲使用的「持有」的修改列表,所以它的remove()方法可行,但其他地方的projectiles領域是不可修改的,現在的迭代的持續時間。

這僅用於調試。一旦你發現問題並糾正它,再次刪除邏輯。

0

檢查ConcurrentModificationException的的文件和ArrayList

https://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html

https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

解決您的問題,請檢查下面的代碼。

例如

import java.util.*; 

public class HelloWorld{ 

    public static void main(String []args){ 
     List<String> animals = new ArrayList<>(); 
     animals.add("Cat1"); 
     animals.add("Cat2"); 
     animals.add("Cat3"); 
     animals.add("Cat4"); 
     animals.add("Cat5"); 

     for(ListIterator<String> iterator = animals.listIterator(); iterator.hasNext();) { 
      String name = iterator.next(); 
      if (name.equals("Cat2")) { 
       iterator.remove(); 
       continue; 
      } 
      System.out.println(name); 
     } 
    } 
} 

乾杯!!!

+0

我看到的唯一區別就是您將Iterator更改爲ListIterator? – jetp250

+0

並不意味着發送該評論,抱歉..無論如何,我嘗試了這一點,它當然有效,但它也適用於正常的迭代器,意味着錯誤不存在。 – jetp250

相關問題