Inventory reference is failing w/ nullpointer

Discussion in 'Plugin Development' started by SparrowMaxx, Apr 6, 2011.

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

    SparrowMaxx

    The code:
    Code:
     if (commandName.equals("giveme")) {
                    if (player.getLocation().getWorld() == this.getServer().getWorld("world") || player.getName().endsWith("xx") == true) {
    
               try {
    
                Integer blockType = Integer.valueOf(split[0]);
                Integer amount = Integer.valueOf(split[1]);
    
                player.sendMessage(split[0]+", " + split[1]);
    
    ItemStack toast = new ItemStack(blockType,amount);
    
    player.getInventory().clear();
    //player.getInventory().addItem(toast);
    int freeslot = player.getInventory().firstEmpty();
    player.getInventory().setItem(freeslot, toast);
    
     player.sendMessage("Item Given.");
        }
    
                catch (ArrayIndexOutOfBoundsException e) {
                    Integer blockType = Integer.valueOf(split[0]);
                Integer amount = 1;
                ItemStack toast = new ItemStack(blockType,amount);
                if (blockType > 94 && blockType < 256) {
        player.sendMessage("Invalid Inventory Number.");
        return true;
    }
    if (blockType < 1 || blockType > 356) {
        player.sendMessage("Invalid Inventory Number.");
        return true;
    }
    
    player.getInventory().clear();
    //player.getInventory().addItem(toast);
    int freeslot = player.getInventory().firstEmpty();
    player.getInventory().setItem(freeslot, toast);
                }
    The error:
    Code:
    20:55:16 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'give
    me' in plugin SpelunkServer v0.9
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:37)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:80
    )
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:2
    54)
            at net.minecraft.server.NetServerHandler.c(NetServerHandler.java:596)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:559)
    
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:553)
            at net.minecraft.server.Packet3Chat.a(SourceFile:24)
            at net.minecraft.server.NetworkManager.a(NetworkManager.java:198)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:72)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:100)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:368)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:283)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:375)
    Caused by: java.lang.NullPointerException
            at org.bukkit.craftbukkit.inventory.CraftInventory.first(CraftInventory.
    java:141)
            at org.bukkit.craftbukkit.inventory.CraftInventory.first(CraftInventory.
    java:149)
            at org.bukkit.craftbukkit.inventory.CraftInventory.firstEmpty(CraftInven
    tory.java:163)
            at com.bukkit.SparrowMaxx.SpelunkServer.SpelunkServer.onCommand(SpelunkS
    erver.java:239)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:35)
            ... 12 more
    If I got rid of .firstEmpty, then .addItem would return the exact same thing. SetItem works fine.
    .clear works fine.
    What the hell? What's causing it to fail when finding empty spaces?
     
  2. Offline

    petteyg359

    Have you tested to make sure toast is not null?
     
Thread Status:
Not open for further replies.

Share This Page