PlayerInteractEvent NullPointerException

Discussion in 'Plugin Development' started by inventorman101, Jun 4, 2013.

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

    inventorman101

    My code works except for one thing. It prints a stacktrace every time an item right clicked is not under the category I want (which has the name "Cougar-Magnem". It gives me a NullPointerException when I do this here is my code.

    Code:
    public class EventListener extends JavaPlugin implements Listener
    {
       
        @EventHandler(priority=EventPriority.NORMAL)
        public void onPlayerUse(PlayerInteractEvent event)
        {
            Player p = event.getPlayer();
         
            if(event.getAction()==(Action.RIGHT_CLICK_BLOCK) || event.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Cougar-Magnem") != (p.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Cougar-Magnem Ammo")))
            {
                p.sendMessage("Fired");
            }
           
           
            else if(event.getAction()==(Action.RIGHT_CLICK_BLOCK) || event.getAction()==(Action.RIGHT_CLICK_AIR))
            {
               
            }
           
        }
     
    }
     
  2. Offline

    afistofirony

    Check if they have anything in their hand first.

    EDIT: i.e. if (p.getItemInHand() != null)
     
  3. Offline

    inventorman101

    I am still getting a NullPointerException when I right click with any other block. Also when I rightclick on a block with air it still prints a message.
     
  4. Offline

    Jnorr44

    Really need the stacktrace in this instance, it may not be the code you have given us.
     
  5. Offline

    inventorman101

    Here is the stacktrace

    Code:
    Caused by: java.lang.NullPointerException
            at com.brendannoble.goldeneye.EventListener.onPlayerUse(EventListener.ja
    va:23)
            at sun.reflect.GeneratedMethodAccessor7.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 16 more
    >
    Jnorr44
     
  6. Offline

    Jnorr44

    Okay, so I need to know which line is 23, and I also need to see your updated code. (You did put in the p.getItemInHand() != null right?)
     
  7. Offline

    inventorman101

    yes I did Jnorr44

    Code:
    public class EventListener extends JavaPlugin implements Listener
    {
     
        @EventHandler(priority=EventPriority.NORMAL)
        public void onPlayerUse(PlayerInteractEvent event)
        {
            Player p = event.getPlayer();
            {
                if (p.getItemInHand() != null)
                {
    [B]    LINE 23[/B]          if(event.getAction()==(Action.RIGHT_CLICK_BLOCK) || event.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Cougar-Magnem") != (p.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Cougar-Magnem Ammo")))
                    {
                        p.sendMessage("Fired");
                    }
                 
                 
                    else if(event.getAction()==(Action.RIGHT_CLICK_BLOCK) || event.getAction()==(Action.RIGHT_CLICK_AIR))
                    {
                     
                    }
                }
            }
        }
    }
     
  8. Offline

    afistofirony

    That if-statement seems to be incorrect, try this:

    if((event.getAction()==(Action.RIGHT_CLICK_BLOCK) || event.getAction() == Action.RIGHT_CLICK_AIR) && p.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Cougar-Magnem") && !p.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Cougar-Magnem Ammo"))

    This will make sure the player right-clicks air or right-clicks a block. It also ensures that the player is holding something with "Cougar-Magnem" in it, but not the respective ammunition.
     
  9. Offline

    inventorman101

    I am still having the issue with any other item giving an error
     
  10. Offline

    Jnorr44

    Not really sure what you are trying to do... but I cleaned up your code somewhat:

    Code:
        @EventHandler(priority = EventPriority.NORMAL) public void onPlayerUse(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            {
                if (p.getItemInHand() != null) {
                    if ((event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) && weirdCheck(p)) {
                        p.sendMessage("Fired");
                    } else if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) {
                        //nothing
                    }
                }
            }
        }
     
        public boolean weirdCheck(Player p) {
            String displayName = p.getItemInHand().getItemMeta().getDisplayName();
            return displayName.contains("Cougar-Magnem") && !displayName.contains("Cougar-Magnem Ammo");
        }
     
  11. Offline

    inventorman101

    Jnorr44 I am going to have it shoot out firework effects where the player is looking and deal damage if it hits another entity. I am also going to have the item have a cooldown time. I keep getting an error in my console any time another item is right-clicked somewhere.
     
  12. Offline

    Jnorr44

    Send me the error...
     
  13. Offline

    inventorman101

    It still give me the same stacktrace that has to do with both methods.

    Code:
    Caused by: java.lang.NullPointerException
            at com.brendannoble.goldeneye.EventListener.weirdCheck(EventListener.jav
    a:32)
            at com.brendannoble.goldeneye.EventListener.onPlayerUse(EventListener.ja
    va:21)
            at sun.reflect.GeneratedMethodAccessor12.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 16 more
    >
     
  14. Offline

    Jnorr44

    If line 32 is :
    Code:
    String displayName = p.getItemInHand().getItemMeta().getDisplayName();
    then the item in hand has no item meta,
    but if line 32 is:
    Code:
    return displayName.contains("Cougar-Magnem") && !displayName.contains("Cougar-Magnem Ammo");
    then the displayName is null for that itemMeta.
     
  15. Offline

    inventorman101

    So what should I do? Because it still does it Jnorr44
     
  16. Offline

    Technius

    inventorman101
    Code:
    //Untested
    //You could change this method name
    public boolean weirdCheck(Player player)
    {
         ItemStack i = player.getItemInHand();
         if(i == null)return false; // Fail the test if there is no item in hand
         if(!i.hasItemMeta())return false; // Fail the test if item does not have metadata
         ItemMeta im = i.getItemMeta();
         if(!im.hasDisplayName())return false; // Fail the test if the item doesn't have a display name
         String dn = im.getDisplayName();
         return dn.indexOf("Cougar-Magnem") > -1 && dn.indexOf("Cougar-Magnem Ammo") > - 1;
    }
    
     
  17. Offline

    inventorman101

    Technius Thank You it works but it says Fired on Cougar-magnem ammo not just Cougar-Magnem Do I just switch the names around?

    EDIT: Also I need this to work for multiple guns not just one so I need to pass in a value somewhere could you put that into the method.

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page