Solved Null Error

Discussion in 'Plugin Development' started by davidclue, Oct 30, 2020.

  1. Offline

    davidclue

    Can anyone explain to me why this code throws a null error?
    Code:
    for (Entity list : p.getNearbyEntities(30D, 30D, 30D)) {
                if (!(list instanceof Mob)) continue;
                Mob e = (Mob) list;
                if (e.getTarget().equals(p)) e.setTarget(null);
                Bukkit.getLogger().info("target = "+e.getTarget());
            }
    Its the last line that sets the target, that line throws a null pointer exception and I don't know why.

    https://hub.spigotmc.org/javadocs/s...tml#setTarget(org.bukkit.entity.LivingEntity)
    The docs say that it is nullable and will set the target to none if put null.

    Nvm I just figured it out so apparently using equals() throws a null error and doesn't work but I changed it with '==' and it worked for some reason.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 30, 2020
  2. Offline

    w0lves

    In which line are u getting the error?

    If it's on

    Code:
    if (e.getTarget().equals(p)) e.setTarget(null);
    Maybe check first than e.getTarget is not null
     
  3. Offline

    KarimAKL

     
  4. Offline

    davidclue

    No getTarget() can return null
     

Share This Page