Very odd NPE on PlayerDeathEvent

Discussion in 'Plugin Development' started by boysnnoco, Feb 27, 2014.

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

    boysnnoco

    So while trying to code for a kitpvp server, when I try to get the killer of a player it always seems to return null. Code:
    Code:java
    1. @EventHandler
    2. public void onDeath(PlayerDeathEvent event){
    3. Player player = (Player)event.getEntity();
    4. plugin.kit.removeAllKit(player);
    5. for(PotionEffect effect : player.getActivePotionEffects()){
    6. player.removePotionEffect(effect.getType());
    7. }
    8. event.setDeathMessage(null);
    9. event.getDrops().clear();
    10. respawnPlayer(player);
    11. Bukkit.broadcastMessage("" + event.getEntity());
    12. if (event.getEntity().getKiller() != null){ //Says the killer is null
    13. if(event.getEntity().getKiller() instanceof Player){
    14. Player killer = (Player) event.getEntity().getKiller();
    15. Inventory inv = killer.getInventory();
    16. for (int i = 0; i < 31; i++)
    17. inv.addItem(new ItemStack(Material.MUSHROOM_SOUP));
    18. }else{
    19. Bukkit.broadcastMessage("Not a player?");
    20. }
    21. }else{
    22. Bukkit.broadcastMessage("NULL"); //Broadcast that the killer is null
    23. }
    24. }

    I am registering the events, and everything else in the plugin works fine.
    I have asked around with other coder friends of mine and they all can't seem to figure this out. If you need anymore code than the code given just comment thanks for helping!
    (I have looked it up on google but it hasn't proven to be helpful)
     
  2. Offline

    ccrama

    boysnnoco would help to know what line the NPE is on ;)\

    //EDIT
    also, your killer might not even be a player, so bukkit will auto send an NPE. Try surrounding that line with

    Code:java
    1. if(e.getEntity().getKiller().getType() == EntityType.PLAYER){
    2. //execute
    3. } else {
    4. return //not a player who killed the entity
    5. }
    6.  
     
  3. Offline

    Nashor

    I believe he is referring to a "Null Pointer Exception". He is receiving a null object when he uses the .getKiller() method.
     
  4. Offline

    ccrama

    Nashor yeah, figured that. I updated my post with a possible solution
     
  5. Offline

    AoH_Ruthless

    boysnnoco
    Well, you don't even need a null check. There is always a killer, albeit it might not be a player as ccrama pointed out. However, you are doing your instance check just fine.

    Try removing the null check entirely.
     
  6. Offline

    ccrama

    AoH_Ruthless would that null check not fix the NPE? There is obviously something null about the killer because boysnnoco said the NPE is on that line.
     
  7. Offline

    boysnnoco

    ccrama AoH_Ruthless Nashor I fixed the problem it was something to do with the event (or have 2 events) so I moved it to a differnt PlayerDeathEvent and it began working fine but thanks for responding!
     
Thread Status:
Not open for further replies.

Share This Page