由於某種原因,Java給了我一個NullPointerException
。我知道這通常很容易解決,但在這種情況下,我無法弄清楚爲什麼它把它放在首位。爲什麼我無法弄清楚?那麼,因爲它沒有堆棧跟蹤!這裏是我的代碼:沒有堆棧跟蹤的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
那麼,什麼是地獄這是否意味着,爲什麼它發生,我該如何解決?
一旦有足夠的異常拋出相同的點,它會刪除堆棧跟蹤。你需要看看你的日誌的開始。 (或者你可以使用一個調試器來產生這個異常的斷點) –
無論lastpos是否爲空,你仍然會這樣做'lastpos = entity.getLocation();' –