NullPointerException没有堆栈跟踪

时间:2016-04-30 15:29:24

标签: java nullpointerexception

Java,出于某种原因,给了我一个NullPointerException。我知道这通常很容易解决,但在这种情况下,我无法弄清楚为什么它首先抛出它。为什么我不明白?好吧,因为它没有堆栈跟踪!这是我的代码:

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(),在开始调用onUse()之前调用onTick(),这是控制台输出:

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

那么这到底是什么意思,它为什么会发生,我该如何修复呢?

1 个答案:

答案 0 :(得分:2)

可能是JVM问题,将此参数传递给它以获得完整的堆栈跟踪:

-XX:-OmitStackTraceInFastThrow

现在,NPE已存在,因为有null的内容,您尝试从中调用方法。可能的候选人是entity,因为您拨打entity.getLocation(),然后entity.getLocation(),因为您拨打entity.getLocation().getBlock(),或者甚至是在致电entity.getLocation().getBlock().getType()时得到的对象。只有lastpos