Why is this null?

Discussion in 'Plugin Development' started by glasseater, Nov 27, 2016.

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

    glasseater

    Hey guys,
    So I am developing a bank note plugin and everything works its just when ever someone right clicks something that isn't the note, I get an NPE on this line

    Code:
    for (String string : e.getPlayer().getItemInHand().getItemMeta().getLore()) {
    I tried adding a check for the material but no luck. Does anyone know what I am doing wrong?
     
  2. Online

    timtower Administrator Administrator Moderator

    @glasseater There might be no item in hand.
    It might not have item meta.
    It might not have a lore.
     
  3. Offline

    glasseater

    @timtower I tried adding a null check but that didn't work either, I also have a check to check if the String is null but no luck :/
     
  4. Online

    timtower Administrator Administrator Moderator

    @glasseater Tiried hasMeta and hasLore after itmeinhand!=null
     
  5. Offline

    glasseater

    @timtower Tried still no luck still an error in the same place :/
     
  6. Online

    timtower Administrator Administrator Moderator

  7. @glasseater Check if item in hand is not null, check hasItemMeta and check hasLore, once you have done this post the updated code.
     
    Last edited: Nov 27, 2016
  8. Offline

    glasseater

    @timtower
    Code:
    if (a.equals(Action.RIGHT_CLICK_AIR) || a.equals(Action.RIGHT_CLICK_BLOCK)) {
                    if (p.getItemInHand() !=null)
                        if(p.getItemInHand().hasItemMeta()) {
                   
                String price = null;
                for (String string : e.getPlayer().getItemInHand().getItemMeta().getLore()) {
                    if (string.startsWith("§ePrice §8> ")) {
     
  9. @glasseater Compare enums with == not .equals
    Also use && to hook multiple checks together, and you never checked for a lore, add hasLore
     
  10. Offline

    glasseater

    @bwfcwalshy Added hasLore no luck :/

    Updated code:

    Code:
    ItemStack hand = e.getPlayer().getInventory().getItemInHand();
            ItemMeta meta = hand.getItemMeta();
         
                    if (a == (Action.RIGHT_CLICK_AIR) && a == (Action.RIGHT_CLICK_BLOCK)) {
                    if (p.getItemInHand() !=null)
                        if(p.getItemInHand().hasItemMeta()) {
                            if(meta.hasLore()) {
    Maybe I structured it wrong?
     
  11. Online

    timtower Administrator Administrator Moderator

    @glasseater You are missing a backet after p.getItemInHand() !=null)
     
  12. @glasseater Your logic has a flaw.

    You do this before checking the meta

    Therefor,
    will throw null if it has no meta
     
Thread Status:
Not open for further replies.

Share This Page