Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a command for a bukkit plugin that lets you transfer worlds. In the section about saving the player's inventory, I'm getting a runtime error. My question is: Why is the error happening, and how can I prevent it?

The plugin code

        public void savePlayerInv(Player p, World w){
        File playerInvConfigFile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getName(), "inventory.yml");
        FileConfiguration pInv = YamlConfiguration.loadConfiguration(playerInvConfigFile);
        PlayerInventory inv = p.getInventory();
        int i = 0;

        for (ItemStack stack : inv.getContents()) {
            //increment integer
            String startInventory = w.getName() + ".inv." + Integer.toString(i);

            //save inv
            pInv.set(startInventory + ".amount", stack.getAmount());
            pInv.set(startInventory + ".durability", Short.toString(stack.getDurability()));
            pInv.set(startInventory + ".type", stack.getTypeId());
            //pInv.set(startInventory + ".enchantment", stack.getEnchantments());
            //TODO add enchant saveing

        i = 0;
        for (ItemStack armor : inv.getArmorContents()){
                String startArmor = w.getName() + ".armor." + Integer.toString(i);

                //save armor
                pInv.set(startArmor + ".amount", armor.getAmount());
                pInv.set(startArmor + ".durability", armor.getDurability());
                pInv.set(startArmor + ".type", armor.getTypeId());
                //pInv.set(startArmor + ".enchantment", armor.getEnchantments());

        //save exp
        if (p.getExp() != 0) {
                pInv.set(w.getName() + ".exp", p.getExp());

The offending line

The stack trace complains about line 130, which is this line.

pInv.set(startInventory + ".amount", stack.getAmount());

The stack trace

2012-03-21 13:23:25 [SEVERE] null
org.bukkit.command.CommandException: Unhandled exception executing command 'wtp' in plugin Needs v1.0
at org.bukkit.command.PluginCommand.execute(
at org.bukkit.command.SimpleCommandMap.dispatch(
at org.bukkit.craftbukkit.CraftServer.dispatchCommand(
at net.minecraft.server.NetServerHandler.handleCommand(
at net.minecraft.server.NetServerHandler.a(
at net.minecraft.server.Packet3Chat.handle(
at net.minecraft.server.NetworkManager.b(
at net.minecraft.server.NetServerHandler.a(
at net.minecraft.server.NetworkListenThread.a(
at net.minecraft.server.MinecraftServer.w(
    Caused by: java.lang.NullPointerException
at com.devoverflow.improved.needs.commands.CommandWorldtp.savePlayerInv(
at com.devoverflow.improved.needs.commands.CommandWorldtp.onCommand(
at org.bukkit.command.PluginCommand.execute(
... 12 more
share|improve this question
You aren't asking anything. – michael.bartnett Mar 21 '12 at 21:16
ohh yeah right... why is the error happening and how can i stop it – HcgRandon Mar 21 '12 at 21:25
You might consider asking this in a Bukkit-centric website/forum. Not many people know Bukkit here. – kaoD Mar 21 '12 at 23:17
Ask the Mod Development forum. – doppelgreener Mar 22 '12 at 0:04
up vote 2 down vote accepted

I have seen this before and I am pretty sure that its because you are getting every item in inventory including the empty ones which return null. inv.getContents() gets everything. Add an if statement inside your for loop to check that they arent null before doing anything with the item.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.