java.lang.IndexOutOfBoundsException错误Bukkit插件

时间:2013-08-05 11:40:25

标签: java plugins bukkit

好的,所以我目前正在开发Bukkit插件,并决定添加一个安排广播的功能。要存储广播,我正在使用config.yml文件并从那里获取广播。我遇到的问题是代码不断返回和IndexOutOfBoundsException,我还没有解决。

这是我的代码:

package uk.co.thek4web.broadcaster;

import java.util.List;
import org.bukkit.scheduler.BukkitRunnable;

public class bcscheduler extends BukkitRunnable {

    private Broadcaster plugin;
    public bcscheduler(Broadcaster plugin){
        this.plugin = plugin;
    }
    private int bcnumber = 0;
    public void run() {
        //String list from config
        List<String> bcm = plugin.message();
        //Broadcasts the next broadcast messsage in the config
        plugin.bc(bcm.get(bcnumber));
        //Makes sure broadcasts don't repeat, goes onto the next broadcast.
        if(bcnumber < bcm.size()){
            bcnumber++;
        }
        else{
            bcnumber = 0;
        }
    }

}

以下是我不断获得的例外情况:

2013-08-04 20:20:43 [WARNING] [Boss Broadcast] Task #4 for Boss Broadcast v0.3 generated an exception
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
    at java.util.ArrayList.rangeCheck(ArrayList.java:603)
    at java.util.ArrayList.get(ArrayList.java:381)
    at uk.co.thek4web.broadcaster.bcscheduler.run(bcscheduler.java:16)
    at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftTask.run(CraftTask.java:53)
    at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
    at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:522)
    at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
    at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)

我已经尝试过一些东西,但是没有一个能够工作,所以任何关于我出错的地方的想法都会受到赞赏。

3 个答案:

答案 0 :(得分:1)

使用

  

if(bcnumber&lt; bcm.size() - 1)

假设大小为3,指数将为0,1,2。考虑bcnumber = 2

时的情况

答案 1 :(得分:1)

使用

public void run() {
  List<String> bcm = plugin.message();
  plugin.bc(bcm.get(bcnumber++));
  bcnumber = bcnumber % bcm.size();
}

答案 2 :(得分:0)

你的问题是你要超出列表大小。顺便提一下关于你的代码的一些建议

 List<String> bcm = plugin.message(); 

// You must check that your list isn't empty before doing your instructions
 if((bcm != null) && !bcm.isEmpty()){
    plugin.bc(bcm.get(bcnumber));


    if(bcnumber < bcm.size()-1){
        bcnumber++;
    }
    else{
        bcnumber = 0;
    }
 }


}