Solved Listener Problems

Discussion in 'Plugin Development' started by PhilDEV_Acc, Apr 25, 2020.

Thread Status:
Not open for further replies.
  1. So I am coding a Plugin to manage my house with some cool features to practise coding. But I got a problem. I coded a Control panel to control the house. So I coded with the PlayerInteractEvent but I alwasy get a NullPointerException and I dont know how to squash it.
    Code:
    Code:Java
    1.  
    2. package creative.listener;
    3.  
    4. import org.bukkit.Location;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Block;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.inventory.ItemStack;
    11.  
    12. import creative.main.CreativeMain;
    13. import creative.main.Utils;
    14.  
    15. public class PlayerInteractEvent implements Listener, Utils {
    16. CreativeMain main;
    17.  
    18. public PlayerInteractEvent(CreativeMain main) {
    19. this.main = main;
    20. }
    21.  
    22. @EventHandler
    23. public void onEvent(org.bukkit.event.player.PlayerInteractEvent e) {
    24.  
    25. Player p = e.getPlayer();
    26.  
    27. //Action action = e.getAction();
    28.  
    29. Block m = e.getClickedBlock();
    30.  
    31. Location sign = (new Location(p.getWorld(), 79, 68, -6));
    32.  
    33. ItemStack stack = e.getItem();
    34.  
    35. if (!(m.getType() == Material.OAK_SIGN)) {
    36. System.out.println(1);
    37. System.out.println(stack);
    38. if (stack.hasItemMeta()) {
    39. System.out.println(2);
    40. if (stack.getItemMeta().getDisplayName().equals("Control Panel")) {
    41. p.openInventory(panelinv());
    42. }
    43. } else {
    44.  
    45. }
    46. }
    47.  
    48. System.out.println(3);
    49. if (m.getType() == Material.OAK_WALL_SIGN) {
    50. Location loc = m.getLocation();
    51. System.out.println(loc);
    52. if (loc.equals(sign)) {
    53. p.getInventory().addItem(panel());
    54.  
    55. }
    56. }
    57.  
    58. }
    59. }
    60.  
    61.  


    Error log:
    Code:
     1
    [15:31:15] [Server thread/INFO]: null
    [15:31:15] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to PhilippsHouse v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:442) ~[spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.PlayerInteractManager.a(PlayerInteractManager.java:441) ~[spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1251) ~[spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:27) ~[spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) ~[spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:918) [spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:911) [spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:895) [spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:828) [spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            at java.lang.Thread.run(Thread.java:834) [?:?]
    Caused by: java.lang.NullPointerException
            at creative.listener.PlayerInteractEvent.onEvent(PlayerInteractEvent.java:37) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
            at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:315) ~[spigot-1.15.2.jar:git-Spigot-ae72bf4-183139d]
            ... 19 more
    
    
    Thanks for your Help ;D
     
  2. Offline

    KarimAKL

  3. Offline

    Tango_

    if (!(m.getType() == Material.OAK_SIGN)) could just be: if(m.getType() != Material.OAK_SIGN)

    and add a null check for both block and item: if(e.getClickedBlock() == null || e.getItem == null) return;
     
  4. thanks that fixed the NullPointerException but why is 'stack' null?
     
  5. Offline

    KarimAKL

    @PhilDEV_Acc Because there was no item in your hand when you clicked.
     
  6. How can I ignore what is in your hand?
     
  7. Offline

    KarimAKL

    By not referencing the item? Am i missing something from your question?
     
Thread Status:
Not open for further replies.

Share This Page