Add items to inventory, gives StackTrace.

Discussion in 'Plugin Development' started by CraftCreeper6, Dec 17, 2014.

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

    CraftCreeper6

    Hi! I am in need of help! I have no been active on the forums much lately, this is my first post in ages. Apart from that; it seems that I cannot add the items from a players inventory. Not too sure why. Here is my code:
    Code:
                Inventory inventory = Bukkit.createInventory(null, 45,
                                      targetPlayer.getName() + "'s Inventory!");
              
                for(ItemStack item : targetPlayer.getInventory().getContents()){
                    //if(item == null) continue; // Makes me unable to use AIR as a material. Unfortunatly.
                    inventory.addItem(item);
                }
    Like the annotation states. Using that; I am unable to use AIR. As I get an error:
    Code:
    [16:34:22 INFO]: CraftCreeper6 issued server command: /is CraftCreeper6
    [16:34:22 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'is'
    in plugin InvSeePlus v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r.java:742) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:956) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :817) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [Craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
    Caused by: java.lang.IllegalArgumentException: Item cannot be null
            at org.apache.commons.lang.Validate.noNullElements(Validate.java:410) ~[
    Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.craftbukkit.v1_7_R3.inventory.CraftInventory.addItem(Craft
    Inventory.java:268) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnk
    s]
            at com.live.craftdev.Commands.onCommand(Commands.java:57) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            ... 13 more
    Line 57:
    Code:
    inventory.addItem(item);
    Thank you for reading. Hope someone can help!
     
  2. Offline

    mythbusterma

    @CraftCreeper6

    It says right there in the stack trace "java.lang.IllegalArgumentException: Item cannot be null," I don't know how it could honestly be any more clear.
     
  3. Offline

    CraftCreeper6

    @mythbusterma
    I know this... As you see in my code, I added a check, although, I do not want the item to be nothing, I want it to be air... It says it in the code as an annotation...
     
  4. Offline

    mythbusterma

    @CraftCreeper6

    If you want to do that, I would recommend making the for loop iterate from 0 to inventory length, and then setting the slot, rather than just add item, that way skipping a slot would allow you to effectively put air in the slot.
     
  5. Offline

    wesley27

    @mythbusterma @CraftCreeper6 While that works, isn't setting it to air essentially just deleting that spot in the inventory? So wouldn't it be a bit simpler to just clear that inventory slot?
     
  6. Offline

    mythbusterma

    @wesley27

    If you set a spot to air, adding an item to the inventory will still try to utilise that space that you set to air, because as far as it's concerned, it's empty.
     
  7. Offline

    teej107

    I don't like how Bukkit doesn't make it clear whether an item is null or AIR. I wish there was a bit more leeway of dealing with AIR slots. An blank spot could be null or AIR. Because of this, I have to perform such checks like
    Code:
    itemStack != null && itemStack.getType() != Material.AIR
    (I got an error thrown when naming an AIR ItemStack, It wasn't null, it was AIR)
    That code bit might be what stops the OPs error from happening.
     
  8. Offline

    PreFiXAUT

    @CraftCreeper6 Well as @mythbusterma said, use the set Method. The best thing would simply be, if you check it it's null or AIR, and set the Item to AIR instead of continuing.

    With this you have the same empty/blank spots like the origin Inventory and you dont have any troubles.
     
Thread Status:
Not open for further replies.

Share This Page