Hello, What I need is, when you right-click a Custom Sword Called "TntSword", a big explosion is created and damages all entities near you. This step works fine, but when I right-click another item / left-click when you don't have an item, a NullPointerException shows up in the console, here is the Code / Error: Code: Code (Java): package loc.joseda.net; import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.block.Block; import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import net.minecraft.server.v1_12_R1.EnumParticle; import net.minecraft.server.v1_12_R1.PacketPlayOutWorldParticles; public class TntSword implements Listener { @EventHandler public void onPlayerInteract(PlayerInteractEvent e){ Player pl = e.getPlayer(); ItemStack item = pl.getInventory().getItemInHand(); String name = item.getItemMeta().getDisplayName(); if (e.getItem() != null && e.getItem().getItemMeta() != null && e.getItem().hasItemMeta()){ if(e.getAction() == Action.RIGHT_CLICK_AIR) { if(name.equals("§cTntSword")) { Block block = pl.getTargetBlock((Set<Material>)null, 8); Location loc = block.getLocation(); float pitch = pl.getEyeLocation().getPitch(); float yaw = pl.getEyeLocation().getYaw(); loc.add(0, 1, 0); loc.setYaw(yaw); loc.setPitch(pitch); pl.teleport(loc); pl.playSound(pl.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 1, 1f); PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(EnumParticle.EXPLOSION_LARGE, true, (float) loc.getX(), (float) loc.getY(), (float) loc.getZ(), 0, 0, 0, 7, 6); for(Player online : Bukkit.getOnlinePlayers()) { ((CraftPlayer)online).getHandle().playerConnection.sendPacket(packet); } final double radius = 10; for (final org.bukkit.entity.Entity entity : pl.getNearbyEntities(radius, radius, radius)) { if (entity instanceof LivingEntity) { final LivingEntity livingEntity = (LivingEntity) entity; if (livingEntity.equals(pl)); livingEntity.damage(30000); } } } } if (e.getItem() != null && e.getItem().getItemMeta() != null && e.getItem().hasItemMeta()){ if(e.getAction() == Action.RIGHT_CLICK_BLOCK) { if(name.equals("§cTntSword")) { Location loc = pl.getLocation(); pl.playSound(loc, Sound.ENTITY_GENERIC_EXPLODE, 1, 1f); PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(EnumParticle.EXPLOSION_LARGE, true, (float) loc.getX(), (float) loc.getY(), (float) loc.getZ(), 0, 0, 0, 7, 6); for(Player online : Bukkit.getOnlinePlayers()) { ((CraftPlayer)online).getHandle().playerConnection.sendPacket(packet); } final double radius = 10; for (final org.bukkit.entity.Entity entity : pl.getNearbyEntities(radius, radius, radius)) { if (entity instanceof LivingEntity) { final LivingEntity livingEntity = (LivingEntity) entity; if (livingEntity.equals(pl)); livingEntity.damage(30000); } } } } } } } } Error: Code (Text): [00:01:40 ERROR]: Could not pass event PlayerInteractEvent to TntSword v1.0.1 org.bukkit.event.EventException: null at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[server.jar:git-Spigot-79a30d7-acbc348] at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[server.jar:git-Spigot-79a30d7-acbc348] at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [server.jar:git-Spigot-79a30d7-acbc348] at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [server.jar:git-Spigot-79a30d7-acbc348] at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:235) [server.jar:git-Spigot-79a30d7-acbc348] at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:202) [server.jar:git-Spigot-79a30d7-acbc348] at net.minecraft.server.v1_12_R1.PlayerInteractManager.a(PlayerInteractManager.java:109) [server.jar:git-Spigot-79a30d7-acbc348] at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:879) [server.jar:git-Spigot-79a30d7-acbc348] at net.minecraft.server.v1_12_R1.PacketPlayInBlockDig.a(SourceFile:40) [server.jar:git-Spigot-79a30d7-acbc348] at net.minecraft.server.v1_12_R1.PacketPlayInBlockDig.a(SourceFile:10) [server.jar:git-Spigot-79a30d7-acbc348] at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [server.jar:git-Spigot-79a30d7-acbc348] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_282] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_282] at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [server.jar:git-Spigot-79a30d7-acbc348] at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [server.jar:git-Spigot-79a30d7-acbc348] at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [server.jar:git-Spigot-79a30d7-acbc348] at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [server.jar:git-Spigot-79a30d7-acbc348] at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [server.jar:git-Spigot-79a30d7-acbc348] at java.lang.Thread.run(Thread.java:748) [?:1.8.0_282] Caused by: java.lang.NullPointerException at loc.joseda.net.TntSword.onPlayerInteract(TntSword.java:33) ~[?:?] at sun.reflect.GeneratedMethodAccessor239.invoke(Unknown Source) ~[?:?] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_282] at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_282] at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[server.jar:git-Spigot-79a30d7-acbc348] ... 18 more Any help is appreciated! Version: 1.12.2
Nullpointer exceptions occur when the event you're passing, is actually null. So your code works fine! To suppress the exception, use a try catch statement. Put the logic in the load, catch the Nullpointerexception. Then you can message the player a message when the nullpointer is thrown, which translates to a message that he is using an empty spot or wrong item.
This is completely wrong, the event cannot be null. The error occurs when the player clicks on e.g. Air, because he accesses the ItemMeta and Air has no ItemMeta.
You need to move your null checks before the variables. So it would be more like this. Code (Java): @EventHandler public void onPlayerInteract(PlayerInteractEvent e){ if (e.getItem() != null && e.getItem().getItemMeta() != null && e.getItem().hasItemMeta()){ Player pl = e.getPlayer(); ItemStack item = pl.getInventory().getItemInHand(); String name = item.getItemMeta().getDisplayName();
It fixes the error while you don't have anything, but If I right-click with another item, I keep getting the NullPointer
I fixed it by placing " if(name.equals("§cTntSword")) { " before " if(e.getAction() == Action.RIGHT_CLICK_AIR) { " Thank you all for the help!
For some reason, Swords you keep after leaving the server and rejoining, keep producing the NullPointer
You can use my ItemUtils class to check if the ItemStack is not null Code (Java): public class ItemUtils { public boolean isValidItem(final @Nullable ItemStack itemStack) { return isValidType(itemStack) && isValidName(itemStack); } public boolean isValidType(final @Nullable ItemStack itemStack) { return itemStack != null && itemStack.getType() != null && itemStack.getType() != Material.AIR; } public boolean isValidName(final @Nullable ItemStack itemStack) { return itemStack != null && itemStack.getItemMeta() != null && itemStack.getItemMeta().getDisplayName() != null; } } if (itemUtils.isValidItem(player.getItemInHand()) {
Do all of your null checks before attempting to reference the things that could be null. You also need to test the item "fingerprint" before you do anything. Using name as the fingerprint is also not a good idea as name is accessible to players to modify via an anvil. Also don't use getItemInHand(), that's deprecated. use getItemInMainHand() instead. What? Nooooooooooo. Error control is not just passing it off to an exception handler. Exception handling should be for edge cases when something really went wonky that you couldn't anticipate. NPE is definitely not something you need to rely on try/catch to resolve.
NullPointers are finally fixed, but, how should I check if the Item is the ItemStack "TntSword" I created in another class? I tried using " item.isSimilar(CustomItems.TntSword) ", It works fine if I get it through the command, but If I change worlds, the item stops working.
Changing world should have no effect on checking an item. I'd have to see the full classes to say for sure. You said it works fine if you get it through the command; what other vector for acquisition is there?
Item in main hand will never be null (can be of type AIR), and getType is annotated as NotNull, so no need to check for null types
There is none other than the Command for now. I finally decided to check itemLore because players can't change that in an anvil, so there is no other way to get the item working than getting the ItemStack by command.