2013-10-06 141 views
0

我正在使用Java編寫代碼作爲Minecraft服務器的插件,但邏輯原理本質上是通用的。Java索引超出範圍錯誤

public void doReviewMember(CommandSender playerSent) { 
    if (!reviewsMember.isEmpty()) { 
     Review doThis = null; 

     ArrayList<Review> players = new ArrayList<Review>(); 
     ArrayList<Review> playersVIP = new ArrayList<Review>(); 
     ArrayList<Review> playersVIPplus = new ArrayList<Review>(); 

     for (int c1 = 0; c1 < reviewsMember.size(); c1++) { 
      if (Bukkit.getPlayer(reviewsMember.get(c1).getName()).hasPermission("reviewplugin.vipplus")) 
       playersVIPplus.add(reviewsMember.get(c1)); 
      else if (Bukkit.getPlayer(reviewsMember.get(c1).getName()).hasPermission("reviewplugin.vip")) 
       playersVIP.add(reviewsMember.get(c1)); 
      else players.add(reviewsMember.get(c1)); 
     } 

     if (playersVIPplus.size() > 0) 
      doThis = playersVIPplus.get(0); 
     else if (playersVIP.size() > 0) 
      doThis = playersVIP.get(0); 
     else doThis = players.get(0); 

     Bukkit.getPlayer(playerSent.getName()).sendMessage("§4[§6ReviewPlugin§4] §eThis review is for §b" + doThis.getName()); 
     Bukkit.getPlayer(playerSent.getName()).teleport(doThis.getLocation()); 
     reviewsMember.remove(doThis); 
     if (reviewsMember.size() > 1) 
      Bukkit.getPlayer(playerSent.getName()).sendMessage("§4[§6ReviewPlugin§4] §eThere are " + reviewsMember.size() + " member reviews left to do."); 
     else if (reviewsMember.size() == 1) 
      Bukkit.getPlayer(playerSent.getName()).sendMessage("§4[§6ReviewPlugin§4] §eThere is " + reviewsMember.size() + " member review left to do."); 
     else 
      Bukkit.getPlayer(playerSent.getName()).sendMessage("§4[§6ReviewPlugin§4] §eThere are no more Member reviews to do at this time!"); 
    } 
    else { 
     Bukkit.getPlayer(playerSent.getName()).sendMessage("§4[§6ReviewPlugin§4] §eThere are no more Member reviews to do at this time!"); 
    } 
} 

索引超出界限的錯誤發生在for循環中,所以我不知道我哪裏出錯了。這是在Windows 8機器上進行測試而沒有出現錯誤,但在Linux中實現時,每次出現索引越界錯誤時都會失敗。

+2

你有一個堆棧跟蹤?錯誤可能更深。 – zch

+0

您是否插入了打印語句以顯示各個點處變量的狀態並準確查看錯誤發生的位置? – abiessu

+2

無關提示:嘗試通過提取一些局部變量來使代碼更容易處理,速度更快:'Review review = reviewsMember.get(c1); Player player = Bukkit.getPlayer(review.getName());'等 – zch

回答

0

上述代碼不能失敗除非某些後臺線程修改了循環中間的數組。這很可能是你的問題的原因。