-3

So i'm making kill counter plugin and i made listener that listens for deaths and kills it looks like this:

@EventHandler
public void killPlayer(EntityDeathEvent e) {

    Entity deadEntity = e.getEntity();
    Entity killer = e.getEntity().getKiller();

    if (killer instanceof Player && deadEntity instanceof Player) {

        Player player = (Player) killer;
        Player dead = (Player) deadEntity;

        int killcount = 0;
        int deathcount = 0;
        String killpath = "playerkills." + player.getName().toLowerCase();
        String deathpath = "playerdeaths." + dead.getName().toLowerCase();


        if (config.contains(killpath)) {
            killcount = config.getInt(killpath);
        }

        if (config.contains(deathpath)) {
            deathcount = config.getInt(deathpath);
        }

        config.set(killpath, killcount  +1);
        config.set(deathpath, deathcount  +1);

        plugin.saveConfig();


    }

I already tried making "pre listener" that would prevent error not happening but it's not working. This is piece of code that i made:

@EventHandler
public void playerDeath(PlayerDeathEvent e){
    Entity entity = e.getEntity();
    EntityDamageEvent entityDamageEvent = entity.getLastDamageCause();
    if(entityDamageEvent.getCause() == EntityDamageEvent.DamageCause.VOID){
        //Player died from void
    }
}

But i still get the same exact error. This is the error:

The error

ThisIsEcho
  • 45
  • 1
  • 1
  • 9
  • 1
    if you read the stacktrace, at the end you get a NullPointerException at line 25 of PlayerDeath class in net.battleheroes.java.deathcost.Events package, we cant help you if you don't post that part of code – Flood2d Apr 24 '17 at 16:03
  • 4
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – tkausl Apr 24 '17 at 16:39
  • 1
    The void is no Entity.. `!= null`is magic – dly Apr 24 '17 at 20:22

1 Answers1

2

The thing is:

Void is not an Entity in Bukkit.

Therefore, your line

 Entity killer = e.getEntity().getKiller();

Throws an error due to void not being an entity.

If it's not an Entity, the getKiller() method returns null!


For a general fix, check if your killer is null before using it.

if (killer != null)//Continue
LeoColman
  • 6,950
  • 7
  • 34
  • 63