ViperLordX ViperLordX - 7 months ago 30
Java Question

NullPointerException with no stack trace

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?

Answer

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 .

Comments