Solved p.getInventory().getBoots().getItemMeta().getDisplayName() Null PointerExc

Discussion in 'Plugin Development' started by deleted_91027365, Apr 20, 2015.

Thread Status:
Not open for further replies.
  1. Hey,

    If I want to check the display name of a Players boot son Move, I became an NullPointerException...

    Code:
    if(p.getInventory().getBoots().getItemMeta().getDisplayName() == "NAME") {
    //Stuff
    }
    
     
  2. @dunklesToast Please learn the basics of Java before trying to make Bukkit plugins, otherwise you tun into problems like this. Bukkit, and Minecraft itself, run on Java, so you can't make plugins before learning it. I recommend the Oracle Java tutorials, or a Java book.
     
  3. Offline

    schwabfl

    - The player could be not wearing boots, meanig that getBoots() will return null and it will throw an NPE if you try getItemMeta()
    - The boots could have no item meta meaning getItemMeta() will return null throwing an NPE
    - The itemmeta could have no displayname meaing getDisplayName() will return null
     
    Last edited: Apr 20, 2015
  4. Offline

    stormneo7

    Code:
    if(p.getInventory().getBoots() != null && p.getInventory().getBoots().hasItemMeta() && p.getInventory().getBoots().getItemMeta().hasDisplayName() && p.getInventory().getBoots().getItemMeta().getDisplayName().equalsIgnoreCase("NAME")){
      // Code
    }
     
  5. Offline

    stormneo7

    Code:
        public static void main(String args[]) {
            System.out.println("a" == "a");
            System.out.println("a" == "b");
            System.out.println("a" == "A");
        }
    Yeah you can. Here's the output
    Code:
    true
    false
    false
     
  6. Offline

    schwabfl

    welp, I'm stupid ._.
    thank you for correcting me :p
     
  7. Offline

    Konato_K

    @schwabfl That is partially wrong, getItemMeta will return the ItemMeta if it has one, or create a new one if it doesn't, it's always better to call hasItemMeta to avoid creating meta of an item that you won't easy.

    However, getItemMeta can return null if the ItemStack is made of AIR.
     
  8. Offline

    Tecno_Wizard

    @schwabfl, no, you're right. In that particular instance when using 2 literals it works, but if you were to use the string constructor it would fail.
     
  9. Offline

    Konato_K

    @stormneo7 You can compare, but it's not always the case, try
    Code:
    new String("hello") == new String("hello")
    It will show false.
     
  10. This is how I fixed it:

    If the Player join first time, he became a pair of boots and so it could not be null.

    Thank you all for your help! The Bukkit Community is amazing! <3
     
  11. Offline

    WingedMLGPro

    Plz mark as solved
     
Thread Status:
Not open for further replies.

Share This Page