2

Java, for some reason, is giving me a NullPointerException. I know that this is usually easy to fix, but in this case, I can't figure out why it's throwing it in the first place. Why can't I figure it out? Well, because it has no stack trace! Here's my code:

package redempt.divinity.ability.modifier.modifiers;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.util.Vector;
import redempt.divinity.ability.AbilityType;
import redempt.divinity.ability.modifier.ProjectileModifier;

public class NoGravity extends ProjectileModifier {
    Vector vector;
    Location lastpos;
    @Override
    public void onUse(Player player, Entity projectile) {
        vector = player.getLocation().getDirection().normalize();
        lastpos = projectile.getLocation();
    }
    @Override
    public String getName() {
        return "No gravity";
    }
    @Override
    public Material getRepresentation() {
        return Material.FEATHER;
    }
    @Override
    public AbilityType getType() {
        return AbilityType.PROJECTILE;
    }
    @Override
    public void onHit(EntityDamageByEntityEvent event) {
    }
    @Override
    public void onTick(Entity entity) {
        try {
            if (lastpos == null) {
                lastpos = entity.getLocation();
            }
            if (entity.getLocation().getBlock().getType().equals(Material.AIR)) {
                entity.setVelocity(vector);
            }
            lastpos = entity.getLocation();
        } catch (Exception e) {
            System.out.println("Exception");
            e.printStackTrace();
        }
    }
}

onTick() is called every tick, onUse() is called before it starts calling onTick(), and here's the console output:

[11:26:52 INFO]: Exception
[11:26:52 WARN]: java.lang.NullPointerException
... this repeats often

So what the hell does this mean, why does it happen, and how do I fix it?

martijnn2008
  • 3,552
  • 5
  • 30
  • 40
ViperLordX
  • 125
  • 1
  • 10
  • 1
    Once you have enough exceptions thrown at the same point, it drops the stack trace. You need to look at the start of your log. (Or you can use a debugger to breakpoint which this Exception occurs) – Peter Lawrey Apr 30 '16 at 15:35
  • Regardless of if lastpos is null, you still do `lastpos = entity.getLocation();` – OneCricketeer Apr 30 '16 at 15:45

1 Answers1

2

Probably issue with JVM, pass this argument to it in order to have full stack trace:

-XX:-OmitStackTraceInFastThrow

Now, NPE is there because something out there is null and you try to call a method from it. Potential candidates are entity since you call entity.getLocation(), then entity.getLocation(), since you call entity.getLocation().getBlock(), or even object you get when calling entity.getLocation().getBlock().getType() Check them out too not only lastpos .

Adnan Isajbegovic
  • 2,227
  • 17
  • 27