正如您在所有println上所看到的那样,我花了很多时间进行故障排除。所以我要问。为什么blocks.add有效,但是labels.add错误为null?

我的疑难解答println返回“添加标签[1]:强度”,因此我知道其中的值。

void load() {
    String path = "menus.inventorys.stats";
    System.out.println("setting variables");
    // now load stat details from config
    System.out.println("default name");
    String defaultName = statConfig.getDefaults().getString(path + ".name");
    System.out.println("done");
    System.out.println("defaultslots");
    int defaultSlots = statConfig.getDefaults().getInt(path + ".slots");
    System.out.println("done");
    System.out.println("mame");
    name = statConfig.getString(path + ".name", defaultName);
    System.out.println("done");
    System.out.println("slots");
    slots = statConfig.getInt(path + ".slots", defaultSlots);
    System.out.println("done");

    InventoryController inventory = new InventoryController();
    ArrayList<Material> blocks = new ArrayList<>();
    ArrayList<String> labels = new ArrayList<>();
    ArrayList<String> slots = new ArrayList<>();

    if (statConfig.isSet(path + "slot")) {
        System.out.println("path to slots exists");
    }

    System.out.println("beginning loop");
    for (String slot : statConfig.getConfigurationSection(path + ".slot").getKeys(false)) {
        System.out.println("iterating material");
        Material block = (Material) statConfig.get(path + ".slot." + slot + ".block");
        System.out.println("material found");
        System.out.println("iterating label");
        String label = statConfig.getString(path + ".slot." + slot + ".label");
        System.out.println("label found");
        System.out.println("adding block");
        blocks.add(block);
        System.out.println("done");
        int blockSize = blocks.size();
        System.out.println("adding labels[" + blockSize + "]: " + label);
        labels.add(blockSize, label);
        System.out.println("done");
        System.out.println("adding slot");
        slots.add(blocks.size(), label);  // remember this is seperate from the arraylist for the block ItemStacks we will need to call this seperately
        System.out.println("done");
    }



[16:17:08] [服务器线程/信息]:已完成
[16:17:08] [服务器线程/信息]:添加标签[1]:强度
[16:17:08] [服务器线程/信息]:已完成
[16:17:08] [服务器线程/信息]:添加插槽
[16:17:08] [服务器线程/错误]:空
org.bukkit.command.CommandException:插件rift v0.0.1中执行命令“ rift”的未处理异常
在org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
〜[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
〜[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:646)
〜[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1133)
[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:968)
[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:45)
[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:1)
[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在net.minecraft.server.v1_8_R2.PlayerConnectionUtils $ 1.run(SourceFile:13)
[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在java.util.concurrent.Executors $ RunnableAdapter.call(未知来源)[?: 1.8.0_51]
在java.util.concurrent.FutureTask.run(未知来源)[?: 1.8.0_51]
在net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60)[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:710)
[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368)
[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651)
[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554)
[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
在java.lang.Thread.run(未知来源)[?: 1.8.0_51]
造成原因:java.lang.IndexOutOfBoundsException:索引:1,大小:0
在java.util.ArrayList.rangeCheckForAdd(未知源)〜[?:1.8.0_51]
在java.util.ArrayList.add(未知来源)〜[?:1.8.0_51]
在com.au.mshcraft.rift.StatModel.load(StatController.java:201)〜[?:?]
在com.au.mshcraft.rift.StatController.open(StatController.java:52)
〜[?:?]
在com.au.mshcraft.rift.Rift.statView(Rift.java:100)〜[?:?]
在com.au.mshcraft.rift.Rift.onCommand(Rift.java:81)〜[?:?]
在org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
〜[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]
...另外15个

最佳答案

由于没有初始化标签ArrayList中的元素,因此出现错误。如果您从索引0开始添加,它将起作用,但是由于尚未为您初始化索引1,它将导致indexoutofbounds异常。

09-05 19:00