Solved onPlayerInteractEvent throwing a NullPointerException error

Discussion in 'Spigot Plugin Development' started by MrMacor, Jan 12, 2020.

  1. Hi, I am currently working on a plugin that will create simple teleporters. The plugin is at the stage where when I click a heavy/light pressure plate after running a command, it will print some dummy text. However, I’m getting a NullPointerException error in my onPlayerInteractEvent event. The stack trace below points to a blank line in code. This time around I’m running the Spigot plugin on a Spigot local server.
    Code (mainclass (Unknown Language)):

    package me.mrmacor.televators;


    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{

    @Override
    public void onEnable() {
    this.getCommand("linkpads").setExecutor(new Linkpads());
    getServer().getPluginManager().registerEvents(new Linkpads(), this);
    }

    @Override
    public void onDisable() {

    }


    }
     
    Here’s the Linkpads class:
    Code (linkpadsclass (Unknown Language)):

    package me.mrmacor.televators;

    import java.util.ArrayList;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    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.EquipmentSlot;

    public class Linkpads implements CommandExecutor, Listener{


    private static ArrayList<String> PlayerLinkpads = new ArrayList<String>(30);



    public boolean onCommand(CommandSender player, Command commmand, String label, String[] args) {

    if (player instanceof Player) {

    if (PlayerLinkpads.contains(player.getName())) {

    // Fired when the player sends the command twice in a row
    player.sendMessage(ChatColor.DARK_RED + "Pad linking cancelled.");
    PlayerLinkpads.remove(player.getName());
    return false;

    } else {

    PlayerLinkpads.add(player.getName());
    player.sendMessage(ChatColor.AQUA + "Please right click the first plate.");
    return false;
    }

    } else {
    player.sendMessage("Only players can execute this command!");
    }

    return false;

    }

    @EventHandler
    public void onPlayerInteractEvent(PlayerInteractEvent e) {

    if (e.getHand().equals(EquipmentSlot.OFF_HAND)) return; // Makes sure the event only fires once

    //**THIS IS WHERE THE STACK TRACE POINTS TO**//

    Player player = e.getPlayer();
    Block block = e.getClickedBlock();
    Action action = e.getAction();


    if (block.getType().equals(Material.HEAVY_WEIGHTED_PRESSURE_PLATE) | block.getType().equals(Material.LIGHT_WEIGHTED_PRESSURE_PLATE) && PlayerLinkpads.contains(player.getName()) && action.equals(Action.RIGHT_CLICK_BLOCK)) {


    //stuff for pad linking
    player.sendMessage("stuff");
    PlayerLinkpads.remove(player.getName());

    } else if (!block.getType().equals(Material.HEAVY_WEIGHTED_PRESSURE_PLATE) | !block.getType().equals(Material.LIGHT_WEIGHTED_PRESSURE_PLATE) | action != Action.RIGHT_CLICK_BLOCK && PlayerLinkpads.contains(player.getName())) {
    // Fired when the player clicks the wrong block
    player.sendMessage(ChatColor.DARK_RED + "Pad linking cancelled.");
    PlayerLinkpads.remove(player.getName());

    }

     
    }

    }
     
    And here is the stack trace:
    Code (stacktrace (Unknown Language)):

    [16:31:23] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to TeleVators v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:429) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:396) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.BlockPressurePlateWeighted.b(BlockPressurePlateWeighted.java:29) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.BlockPressurePlateAbstract.a(BlockPressurePlateAbstract.java:69) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.BlockPressurePlateAbstract.a(BlockPressurePlateAbstract.java:62) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IBlockData.a(SourceFile:273) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.Entity.checkBlockCollisions(Entity.java:922) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.Entity.move(Entity.java:626) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:920) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInFlying.a(SourceFile:126) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInFlying$PacketPlayInPosition.a(SourceFile:57) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:910) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:903) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeAll(SourceFile:103) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:886) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:820) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]
    Caused by: java.lang.NullPointerException
            at me.mrmacor.televators.Linkpads.onPlayerInteractEvent(Linkpads.java:55) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_211]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_211]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_211]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_211]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
     
    Any help would be much appreciated, thanks!!
     
  2. the clicked block can be null. also, in your first if statement of the event, you have [boolean statement] | [boolean statement], but it should be [boolean statement] || [boolean statement]
     
  3. I’ve added the following line of code to try and prevent the event from firing when the clicked block is null:
    Code (Text):

    if (e.getClickedBlock() == null) return;
     
    ... and the issue still occurs.
    How should I go about stopping the event from firing? I’m a bit new to events. :p
     
  4. if the issue still occurs after that, then the error should update with a new line. whats the new error?

    /e also post updated code
     
    • Agree Agree x 1
  5. I figured out that the error was me not understanding how to properly null check, apparently. xD
    According to this post (https://bukkit.org/threads/nullpointerexception-error.445179/#post-3483354), you should not use:
    Code (Text):

    if (e.getClickedBlock().equals(null)) return;
     
    but rather:
    Code (Text):

    if (e.getClickedBlock() == null) return;
     
    After adding and fixing null checks to all of the variables that the event provides and I use in my logic, I no longer get the NullPointException error. Thanks for helping me! (I apologize if this was confusing, I know I mentioned the correct way to null check before but I forgot due to a lack of sleep.) :)