Null player inventories

Discussion in 'Plugin Development' started by Proteh, Mar 3, 2012.

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

    Proteh

    Hello there. I'm having trouble manipulating a player's inventory. It always gives me a NullPointerException with the latest dev build, and this was working fine in 1.1.

    Here's some example code:

    Code:
    @EventHandler
    public void whenPlayerJoins(PlayerJoinEvent event) 
    {
         Player player = event.getPlayer();
         Inventory playerinv = player.getInventory();
         for(int j = 0; j < playerinv.getSize(); j++)
         {
              playerinv.getItem(j).setAmount(64);
         }
    }
    
    It will give a NullPointerException, try it.

    Does anyone have a workaround for this? Thanks in advance.
     
  2. Offline

    nisovin

    Check to make sure the item isn't null before trying to manipulate it. You can't set an empty slot to have 64 items in it.
     
    Proteh likes this.
  3. Offline

    Proteh

    The code was more complicated than that, it was first checking if the slot had, let's say, some diamonds, for example.
    Anyways, thanks for pointing that out, that fixed my problem!
     
  4. Offline

    troed

    R5/R6 (and thus 1.2.3-Rx) seems to have changed the general behaviour of empty slots from containing (I guess) Material.AIR to now containing null Items instead. This has caused code to trip up for my plugin, with a lot of tickets now being posted.

    I'm assuming it's the same as happened here:

    In my case it's the PlayerItemHeldEvent that has changed. Previous working code:

    Code:
    if(e.getPlayer().getInventory().getItem(e.getNewSlot()).getType() == Material.MAP) {
    ...
    Needed to be changed to:

    Code:
    ItemStack item = e.getPlayer().getInventory().getItem(e.getNewSlot());
    if(item != null && item.getType() == Material.MAP) {
    ...
    I haven't seen this communicated anywhere, and I guess everyone who has assumed slots didn't contain NULL before will notice the change anyway ...
     
  5. Offline

    stelar7

    before all empty itemslots had an AIR material, now it is simply null
     
  6. :confused: when was it Material.AIR ?
    I always had null returned for unexistent items in inventories when I developed RecipeManager, that was before CB #1337 was released xD
     
  7. Offline

    stelar7

    there was an anouncement for it some time back, can't find it now tho...
     
  8. Oh, so it was added and then removed ? =)
     
Thread Status:
Not open for further replies.

Share This Page