简体   繁体   中英

why does variable.add(index, element) create a null error?

I have spent a lot of time troubleshooting this as you can see by all the println's. So I need to ask. Why is blocks.add working but labels.add I get error null?

My troubleshooting println returns "adding labels[1]: Strength" so I know the values are in there.

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] [Server thread/INFO]: done [16:17:08] [Server thread/INFO]: adding labels[1]: Strength [16:17:08] [Server thread/INFO]: done [16:17:08] [Server thread/INFO]: adding slot [16:17:08] [Server thread/ERROR]: null org.bukkit.command.CommandException: Unhandled exception executing command 'rift' in plugin rift v0.0.1 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214] at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214] at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1133) [spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214] at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:968) [spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214] at net.minecraft.server .v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214] at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214] at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8_server.jar:git-Spigot-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) [spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214] at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214] at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot-1.8_server.jar:git-Spigot-dbe012b-61ef21 4] at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214] at java.lang.Thread.run(Unknown Source) [?:1.8.0_51] Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.ArrayList.rangeCheckForAdd(Unknown Source) ~[?:1.8.0_51] at java.util.ArrayList.add(Unknown Source) ~[?:1.8.0_51] at com.au.mshcraft.rift.StatModel.load(StatController.java:201) ~[?:?] at com.au.mshcraft.rift.StatController.open(StatController.java:52) ~[?:?] at com.au.mshcraft.rift.Rift.statView(Rift.java:100) ~[?:?] at com.au.mshcraft.rift.Rift.onCommand(Rift.java:81) ~[?:?] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8_server.jar:git-Spigot-dbe012b-61ef214] ... 15 more

You are getting the error because you have not initialised the elements in your labels ArrayList. It will work if you start adding from index 0 , but since index 1 has not been initialised for you, it will cause indexoutofbounds exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM