Solved item always null

Discussion in 'Plugin Development' started by pkt, Jun 21, 2013.

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

    pkt

    I have been constantly been getting a NullPointerException, no matter what code I use. I haven't coded in Java for a couple months, so I am a bit rusty, can anyone help me out?

    Code:
            for (ItemStack items : player.getInventory().getContents()) {
                if (items != null)
                    if (items.hasItemMeta() && items.getType() == Material.WRITTEN_BOOK)
                        if (((BookMeta) items.getItemMeta()).getTitle().equals(ChatColor.BLUE + "test"))
                            items.setTypeId(0);
            }
     
  2. Offline

    chasechocolate

    Which line are you getting the NPE on?
     
  3. Offline

    pkt

    all of the ones in the for loop, it doesn't matter what order I put them in, they all get nulls
     
  4. Offline

    chasechocolate

    pkt is player null? :3
     
  5. Offline

    pkt

    no, just the items

    Why does items.equals(null) give an npe when I'm checking to see if its null? And whats the difference between using == and .equals() ? one seems to work sometimes and the other doesnt...?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  6. Offline

    foodyling

    You can use .equals() on a null since a null object doesn't have any specific functions. Add some debug such as
    Code:
    if (player == null) {
      getLogger().info("Player is null");
    }
    if (player.getInventory() == null) {
      getLogger().info("Inventory is null");
    } 
    to find the root of the NPE
     
  7. Offline

    pkt

    I already did debugging.
    For some reason... it just stopped giving npes.... I did not change any code or order.... but now my problem is removing the item, item.setType() to AIR or 0 won't work..
     
  8. Offline

    foodyling

    Player's can't carry an air block, if I recall correctly you might be able to do player.getInventory().remove(items)
     
  9. Offline

    pkt

    Yea, just found that method in the JavaDocs... I remember setting items to air a long time ago.. :eek: I really need to keep up with Java, been away soo long. well thanks for helping!
     
    foodyling likes this.
Thread Status:
Not open for further replies.

Share This Page