Item in inventory is null

Discussion in 'Plugin Development' started by Ebbez, May 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Ebbez

    Hello Guyz
    I'm coding something but when i call this void there occurs a error.
    Code:java
    1. // Set inventory
    2. Inventory inv = chest.getInventory();
    3. int i = 0;
    4. while(i <= 26){
    5. if(inv.getItem(i).getType() == null){
    6. Main.instance.getConfig().set("chests.chest" + chestNumber + ".inv." + i, 0);
    7. }else{
    8. Main.instance.getConfig().set("chests.chest" + chestNumber + ".inv." + i, inv.getItem(i).getTypeId());
    9. }
    10. i++;
    11. }

    I think there occurs an error when i = air.
    Error:
    Code:
    [16:13:07 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'setr
    estockingchest' in plugin Assassins v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r.java:701) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:956) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :817) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    Caused by: java.lang.NullPointerException
            at acp.chests.Utils.createRestockingChest(Utils.java:30) ~[?:?]
            at acp.main.Main.onCommand(Main.java:55) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
            ... 13 more
     
  2. Remove the getType() in the first if statement in the while loop.
     
    Ebbez likes this.
  3. Offline

    Chlorek

    Yea, when there is no item you get null from getItem(), and it causes NullPointerException when you want to call some member stuff from than null-object. It's like:
    *null*.doSomething();

    There is no instance, so it won't work.
     
  4. just do
    Code:java
    1.  
    2. for (int i = 0; i < inv.getSize(); i++) {
    3. if (inv.getItem(i) == null) {
    4. Main.instance.getConfig().set("chests.chest" + chestNumber + ".inv." + i, 0);
    5. } else {
    6. Main.instance.getConfig().set("chests.chest" + chestNumber + ".inv." + i, inv.getItem(i).getTypeId());
    7. }
    8.  

    if the item is null you cant get the type of it
     
  5. Offline

    Ebbez

    Thanks for all the help, it solved my problem.
    My code is now:
    Code:java
    1. Inventory inv = chest.getInventory();
    2. int i = 0;
    3. while(i <= 26){
    4. if(inv.getItem(i) == null){
    5. Main.instance.getConfig().set("chests.chest" + chestNumber + ".inv." + i, 0);
    6. }else{
    7. Main.instance.getConfig().set("chests.chest" + chestNumber + ".inv." + i, inv.getItem(i).getTypeId());
    8. }
    9. i++;
    10. }
     
Thread Status:
Not open for further replies.

Share This Page