[SOLVED] ItemStack getTypeID() problem

Discussion in 'Plugin Development' started by EdTheLoon, Jul 13, 2011.

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

    EdTheLoon

    I am getting the below error message in my plugin


    Code:
    13:23:57 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'gi' in plugin Gold2iConomy v0.3
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:37)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:129)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:298)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:711)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:676)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:669)
        at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:84)
        at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
        at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:451)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:361)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    Caused by: java.lang.NullPointerException
        at me.areid.gold2iconomy.gold2iconomy.onCommand(gold2iconomy.java:90)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:35)
        ... 12 more
    
    According to the NullPointerException the problem is occuring on line 90 of my main class. The relevant bit of code is:

    Code:
    // Convert all gold
                if (args[0].equalsIgnoreCase("all")) {
                    Player player = (Player)sender;
                    PlayerInventory pi = player.getInventory();
                    ItemStack items[] = pi.getContents();
    
                    if (pi.contains(266)) {
                        Integer ingots = 0;
                        Integer typeID = 0;
                        Integer amount = 0;
    
                        for (ItemStack item: items) {
                            typeID = item.getTypeId();
                            amount = item.getAmount();
                            // DEBUG ONLY:
                            log.info(ingots.toString() + ", " + typeID.toString()+ ", " + amount.toString());
                            if (typeID == 266) {
                                ingots = ingots + amount;
                            }
                        }
                        convertGold(sender, ingots);
                        return true;
                    } else {
                        sender.sendMessage(ChatColor.DARK_RED + "You don't have any gold ingots to convert!");
                        return true;
                    }
                }
    When command is run in-game I get the error after it tries to perform this line:
    typeID = item.getTypeId();

    Any ideas?
     
  2. Offline

    Trc202

    Looks like you are getting the item id of an empty inventory slot. Try if item != null inside your for loop.(Untested)
     
  3. Offline

    darklust

    Change the Integer objects to primitive int types.
     
  4. Offline

    EdTheLoon

    Does PlayerInventory.getContents() pass empty inventory slots into the item stack? :O
    I shall alter it and give it a try
     
  5. Offline

    Hretsam

    Yes, if a slot is empty its set to null.
     
  6. Offline

    EdTheLoon

    Wow...it worked haha. I'll have to remember that getContents() DOES NOT check for an empty slot before returning the ItemStack. Thanks a lot :)
     
Thread Status:
Not open for further replies.

Share This Page