为什么variable.add(index,element)会创建一个空错误?

时间:2015-10-05 05:30:43

标签: java

我已经花了很多时间对其进行故障排除,因为你可以看到所有的println's。所以我需要问一下。为什么blocks.add工作但label.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] [服务器线程/错误]:null       org.bukkit.command.CommandException:未处理的异常执行命令&#39; rift&#39;在插件rift v0.0.1中         在org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)   〜[插口1.8_server.jar:GIT-插口dbe012b-61ef214]         在org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)   〜[插口1.8_server.jar:GIT-插口dbe012b-61ef214]         在org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:646)   〜[插口1.8_server.jar:GIT-插口dbe012b-61ef214]         at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1133)   [插口1.8_server.jar:GIT-插口dbe012b-61ef214]         at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:968)   [插口1.8_server.jar:GIT-插口dbe012b-61ef214]         at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:45)   [插口1.8_server.jar:GIT-插口dbe012b-61ef214]         at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:1)   [插口1.8_server.jar:GIT-插口dbe012b-61ef214]         at net.minecraft.server.v1_8_R2.PlayerConnectionUtils $ 1.run(SourceFile:13)   [插口1.8_server.jar:GIT-插口dbe012b-61ef214]         at java.util.concurrent.Executors $ RunnableAdapter.call(Unknown Source)[?:1.8.0_51]         at java.util.concurrent.FutureTask.run(Unknown Source)[?:1.8.0_51]         at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60)[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214]         at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:710)   [插口1.8_server.jar:GIT-插口dbe012b-61ef214]         at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368)   [插口1.8_server.jar:GIT-插口dbe012b-61ef214]         at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651)   [插口1.8_server.jar:GIT-插口dbe012b-61ef214]         at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554)   [插口1.8_server.jar:GIT-插口dbe012b-61ef214]         在java.lang.Thread.run(未知来源)[?:1.8.0_51]       引起:java.lang.IndexOutOfBoundsException:索引:1,大小:0         at java.util.ArrayList.rangeCheckForAdd(Unknown Source)〜[?: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)   〜[插口1.8_server.jar:GIT-插口dbe012b-61ef214]         ......还有15个

1 个答案:

答案 0 :(得分:1)

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