Reputation: 13
I try to code that if I right click on a Sign I will teleport to a position. It is working BUT in the Console I get an Error and idk why. This is my code:
public class schildJNR implements Listener {
@EventHandler
public void schildKlickEvent(PlayerInteractEvent event) {
Player player = event.getPlayer();
if(event.getPlayer() instanceof Player && event != null) {
if (event.getClickedBlock().getType() == Material.SPRUCE_WALL_SIGN) {
Sign sign = (Sign) event.getClickedBlock().getState();
if (sign.getLine(0).equalsIgnoreCase("Die Schlucht")) {
player.teleport(new Location(Bukkit.getWorld("world"), 295.14f, 69, 46.54f, -90.1f, 0.9f));
} else if (sign.getLine(0).equalsIgnoreCase("Schatzsuche")) {
player.teleport(new Location(Bukkit.getWorld("world"), 74.59f, 63, 237.51f, -91.4f, 1.3f));
} else if (sign.getLine(0).equalsIgnoreCase("Fichtenwald")) {
player.teleport(new Location(Bukkit.getWorld("world"), -5.43f, 78, 172.57f, 179.7f, 1.6f));
}
}
} else {
player.sendMessage("ERROR");
}
}
The error is: org.bukkit.event.EventException: null [...] Caused by: java.lang.NullPointerException
What am I making wrong?
Upvotes: 0
Views: 875
Reputation: 13
ok I fixed it. I tried to execute the 'getClickedBlock()' method but if I for example punch in the air the 'PlayerInteractEvent' will be execute and then the code is trying to execute this: 'if (event.getClickedBlock().getType() == Material.SPRUCE_WALL_SIGN) {' but I doesn't klicked a block so there will be an error. I added a if condition and now it is working.
Upvotes: 0
Reputation: 1078
Your order of expressions in the if is incorrect, you need to first check if it's null and then if it is instanceof player.
if(event != null && event.getPlayer() instanceof Player) {
Upvotes: 2