Error when I right-click air.

Discussion in 'Spigot Plugin Development' started by Zarlen, May 5, 2017.

  1. Hello,

    Today I was getting an error with an event (it was caused by the following code - the code was on line 33, as shown in the console).
    Code (Text):
    public void createBowls(PlayerInteractEvent event) {
    if (event.getClickedBlock().getType() == Material.IRON_FENCE) {
    if (this.getConfig().get("userdata." + event.getPlayer().getName() + ".team").equals("dwarf")) {
    if (this.getConfig().get("server.general.gamestarted").equals("enabled")) {
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    if (event.getPlayer().getItemInHand().getType() == Material.STICK) {
    ItemStack bowl = new ItemStack(Material.INK_SACK, 1, (short) 8);
    ItemMeta bowlmeta = bowl.getItemMeta();
    bowlmeta.setDisplayName(ChatColor.AQUA + "Bowl");
    bowlmeta.setLore(Arrays.asList("Use these on the oil Jimmy. We are gonna need more", "morter for these walls to make them strong, Jimmy!", ChatColor.GOLD + "- BruceWillakers"));
    Shrinebar.removeItemType(event.getPlayer(), Material.STICK, 1);
    Shrinebar.playSound(event.getClickedBlock().getLocation(), Sound.ZOMBIE_REMEDY, 1, 2);
    And the console says it's caused by:
    Code (Text):
    if (event.getClickedBlock().getType() == Material.IRON_FENCE) {
  2. You need to add a null check, since when you click air e#getblockclicked return null (And please, do some formatting at that code, it gave me eye cancer)
  3. But in my previous plugins (deleted already) I did the same and didnt get a error.
  4. Maybe because you need to add the ignoreCancelled=true to the event handler (since when click air it dire as already cancelled) set @EventHandler(ignoreCancelled=true) and should work fine
  5. Code (Text):
    [22:48:36 ERROR]: Could not pass event PlayerInteractEvent to Game v1.0
    va:310) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInte
    ractEvent( [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInte
    ractEvent( [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInte
    ractEvent( [server.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(
    :717) [server.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlo [server.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlo [server.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$
    3) [server.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$ Source) [
            at Source) [?:1.8.0_131]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [server.jar
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(
    15) [server.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(
    74) [server.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(
    54) [server.jar:git-Spigot-21fe707-e1ebe52]
    :557) [server.jar:git-Spigot-21fe707-e1ebe52]
            at Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
            at ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
    va:306) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            ... 18 more
  6. I don't see something wrong since I always use this method without errors.
  7. Without the code i can't help you, show me the new code
  8. look in the post
  10. Just add the damn null checks, like if clicked block is null
  11. But thats useless (in my eyes) because if I use .getClickedBlock().getType() == blabla it SHOULD ONLY DO ACTIONS WITH THAT BLOCK

    I still dont know why I never got this before... I just added some code cleanups (removed the && things etc) and this happend.
  12. Ok good luck :D
  13. You need to open your eyes then.

    Doesn't matter. You clearly don't understand what is erroring. The NPE you're receiving is caused by the ".getType()", the condition doesn't even get evaluated because it errors half way through. The expression you're trying to do is the equivalent of writing "if (null.getType() == xyz)". You can't do that.

    Either you weren't interacting with air or you did indeed have null checks.
    #13 Scarsz, May 5, 2017
    Last edited: May 5, 2017
    • Agree Agree x 1
  14. I tried to help you in two different way, you can choose one of them. Or maybe you can repeat yourself everything's fine and continue to your way
  15. Add null checks. It's been mentioned before.

    Sent from my iPhone using Tapatalk