NullPointerException - No idea where it comes from

Discussion in 'Plugin Development' started by Gabum, Aug 28, 2014.

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


    Hello Bukkiteers!
    I'm searching for help about an exception:

    1. if (e.getDamager() instanceof Projectile){
    2. Projectile projectile = (Projectile) e.getDamager();
    3. if (projectile.getShooter() instanceof LivingEntity){
    4. LivingEntity de = (LivingEntity) projectile.getShooter();
    6. ...
    8. }
    9. }

    The StackTrace says there is a NullPointerException at line Projectile projectile = (Projectile) e.getDamager();. I don't know why this code produces a NullPointerException, because the Projectile should be the Entity that is damaging the damaged Entity. Isn't it?

    Note: The whole code is inside a EntityDamageByEntityEvent.
  2. Offline


    Update Craftbukkit.
  3. Offline


    I'm using the recent dev-build, and I downdated to
    1.7.9-R0.2 (Beta Build). still same error, at the same line :/
  4. Offline


    Do some debugging.
    1. getLogger().warning("Projectile : " + e.getDamager());

    As well as at some other lines. Paste output. :)
  5. Offline


    eeeeehm... Where should I put it, and how do I use it? I never used getLogger() before :D
  6. Offline


    Assuming you have a proper constructor for this listener, simply appending whatever pointer you have towards your JavaPlugin extended class will output text to console.

    If inside of your JavaPlugin extended class, you can simply say: getLogger().
  7. Offline


    Try this. Not only does it look cleaner, it should point to a better line if an error happens.
    2. public void EntityDamageByEntity(EntityDamageByEntityEvent event) {
    3. Entity damager = event.getDamager();
    4. if (!(damager instanceof Projectile))
    5. return; // not a projectile, ignore.
    6. Projectile projectile = (Projectile) damager;
    7. LivingEntity shooter = projectile.getShooter(); // no need to cast..
    8. // do stuffs...
    9. }
  8. Offline


    Well, the instanceof check works effectively like a null check, so e.getDamager() can't be null (and is thus castable), e can't be null either, seeing as it is used in the previous line without problems. So I'm thinking the plugin you have on the server is outdated, and the stacktrace is really pointing to some other line.

    I suggest you rebuild the plugin and test again to see if you get another stacktrace this time.
    Necrodoom likes this.
  9. Offline


    Try to null-check getShooter() since it can throw null, according to the Javadocs:
    "Retrieve the shooter of this projectile. The returned value can be null for projectiles shot from a Dispenser for example."
  10. Offline


    Konato_K So if getShooter() does in fact return null, there still isn't any logical place in the code where a NullPointerException would occur.
Thread Status:
Not open for further replies.

Share This Page