Unable to get players item which is in hand.

Discussion in 'Plugin Development' started by Mee8a, Aug 25, 2015.

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

    Mee8a

    Hey all,
    This code throws an NPE on line 21.

    Im not sure why, any help is appreciated!

    Code:
        @EventHandler
        public void onMessage(AsyncPlayerChatEvent e) {
            if(e.getMessage().contains("[item]")) {
                Player p = (Player) e.getPlayer();
                String item = p.getItemInHand().getItemMeta().getDisplayName();
             
                PlayerInventory pi = p.getInventory();
             
                e.getMessage().replace("[item]", pi.getItemInHand().getItemMeta().getDisplayName()); //Line21
            }
        }
    Error (open)

    Code:
    Caused by: java.lang.NullPointerException
        at java.lang.String.replace(Unknown Source) ~[?:1.8.0_45]
        at me.Mee8a.chatutils.ItemInput.onMessage(ItemInput.java:21) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit-1.8.7.jar:git-Bukkit-7723b90]
        ... 36 more
     
    Last edited: Aug 25, 2015
  2. Offline

    mine-care

    Player p =(Player) e.getPlayer();
    Casting a Player to a Player? The point is?
    Edit: the same aplies for your signature ;/


    Does the item have item meta ? if yes, Does it have a display name?

    Why do you create a variable with the name if you are to use this:

    ??

    you need to wrap up your code...
     
  3. Offline

    Mee8a

    For the PlayerInventory pi thing, That was just me testing It doenst need to be their.
     
  4. Offline

    mine-care

    @Mee8a That isnt the problem as i said,
    if any of the above conditions is false, you will get null or NPE, For instance if the item has no ItemMeta, then, there you got a null and furthermore an NPE if the name is null then the message will just say "null"


    I am still curius about that ^
     
  5. Offline

    Mee8a

    @mine-care

    Yes the item does have ItemMeta. And I have fixed the other issue.
     
  6. Offline

    mine-care

    @Mee8a Are you sure? it doesnt seem to. Debug.
     
  7. @Mee8a Use hasItemMeta and hasDisplayName
     
    mine-care likes this.
  8. Offline

    MCMatters

    @Mee8a no originality these days. cough cough cosmicpvp
     
  9. Offline

    Mee8a

    :)
    I have some original ideas. But I need to do this first.

    @bwfcwalshy
    Their is no hasDisplayName...
     
  10. Offline

    oceantheskatr

    First check if it has item meta, then check if it has a display name, then do what you're doing.

    p.getItemInHand().getItemMeta().hasDisplayName();

    Gotta look around a bit ;)
     
    bwfcwalshy likes this.
Thread Status:
Not open for further replies.

Share This Page