Solved Null Error

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

  1. Offline


    Can anyone explain to me why this code throws a null error?
    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.
    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


    In which line are u getting the error?

    If it's on

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


  4. Offline


    No getTarget() can return null

Share This Page