NullPointerException and ClassCastException

Discussion in 'Spigot Plugin Development' started by PCPSells, Jun 29, 2016.

  1. For some reason my console is constantly SPAMMED every single time a player even interacts with any blocks, from breaking it to right-clicking the error.. legit any interaction.

    Here' the section of code where I constantly get it

    Code (Text):
      public void onSignClick(PlayerInteractEvent e) {
        Sign s = (Sign) e.getClickedBlock().getState();
        Player p = e.getPlayer();
        if (e.getAction() == null) {
            e.setCancelled(true);
            return;
        }
     
  2. You are instantly casting every block every player clicks to a sign. You have to first check if the block being clicked actually IS a sign. Use this:
    Code (Text):
    if(e.getClickedBlock().getState() instanceof Sign) {
        // rest of your code
    }
    This will fire every time a person interacts with a sign.
     
  3. Hey, yeah I wanted to use this, but I use the "s"
    for things like
    Code (Text):
    s.getLine(0).equalsIgnoreCase("kappa, code here, kappa")
    So how would I get
    Code (Text):
     e.getClickedBlock().getState() instanceof Sign
    to be able to be in a Sign format like
    Code (Text):
    Sign s = (Sign) e.getClickedBlock().getState();
     
  4. Just copy the code that you originally posted and paste it into the if-statement that I posted. You can still cast the block to a sign like you want, you just have to check that the block being interacted with actually is a sign, like I showed you.
     
  5. Well I feel dumb. Kappa
     
  6. Another issue,
    Code (Text):
      public void onSignClick(PlayerInteractEvent e) {
        if (e.getClickedBlock().getState() instanceof Sign) {
        Sign s = (Sign) e.getClickedBlock().getState();
        Player p = e.getPlayer();
        if (e.getClickedBlock() == null) {
            e.setCancelled(true);
            return;
        }
    /// my code here
    Is all of what I have, except I'm still getting this error in my console
    Code (Text):
    Could not pass event PlayerInteractEvent to MyPlugin v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1200) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:18) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:1) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_91]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_91]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
     
     
  7. Move the last if to the top and don't cancel the event in that if.
     
  8. Check before if block != null or if action equals rightclickBlock or left clickblock.
     
  9. You're checking a block that you don't even know exists. Every time you interact with the air, you'll get this error because the block you're checking is null. @ArsenArsen has the right idea.
     
    • Like Like x 1
    • Agree Agree x 1