-2

好的,所以我目前正在开发一个 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)

我已经尝试了一些东西,但它们都没有奏效,所以任何关于我到底哪里出错的想法都将不胜感激。

4

3 回答 3

1

利用

if(bcnumber < bcm.size()-1)

假设大小为 3,索引将为 0、1、2。考虑 bcnumber = 2 的情况

于 2013-08-05T11:44:03.087 回答
1

利用

public void run() {
  List<String> bcm = plugin.message();
  plugin.bc(bcm.get(bcnumber++));
  bcnumber = bcnumber % bcm.size();
}
于 2013-08-05T11:47:28.150 回答
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;
    }
 }


}
于 2013-08-05T12:00:20.963 回答