Event casting to wrong Listener

Discussion in 'Plugin Development' started by arnie231, Jun 4, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    arnie231

    ok so I have 2 Seperate Listeners that both listen for the Player interact one is for clicking a sign the other is for right click with an ender pearl in your hand heres my code and the Error

    Thanks for your help in advance

    SignClickListener
    Code:java
    1.  
    2. package com.arnie.sh2.Listeners;
    3.  
    4.  
    5. import com.arnie.sh2.Main;
    6. import com.arnie.sh2.Util.Generate;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.block.Block;
    10. import org.bukkit.block.Sign;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.EventPriority;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.block.Action;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17.  
    18.  
    19. public class SignGeneratorListener implements Listener
    20. {
    21. @EventHandler(priority=EventPriority.NORMAL)
    22. public void OutpostClick(PlayerInteractEvent event){
    23. Action action = event.getAction();
    24. Player player = event.getPlayer();
    25. Block block = event.getClickedBlock();
    26. Material blocktype = block.getType();
    27.  
    28. if (action == Action.RIGHT_CLICK_BLOCK && block != null) {
    29. if (blocktype == Material.SIGN_POST) {
    30. if (Main.config.getBoolean("Stronghold2.Economy.Enabled"))
    31. if (Main.economy.hasAccount(player.getName())) {
    32. double geld = Main.economy.getBalance(player.getName());
    33. double preis = Main.config.getDouble("Stronghold2.Sign.Price");
    34. if (geld >= preis) { Main.economy.depositPlayer(player.getName(), Main.config.getInt("Stronghold2.Sign.Price") * -1);
    35.  
    36. Sign sign = (Sign)block.getState();
    37. if (sign.getLine(0).equalsIgnoreCase(Main.config.getString("Stronghold2.Sign.Name"))) {
    38. if (sign.getLine(1).equalsIgnoreCase("Outpost")){
    39. Generate.Outpost(player);
    40. block.setType(Material.AIR);
    41. player.sendMessage(ChatColor.GOLD + "[Stronghold2]" + ChatColor.RED + "Poof.. You created an Outpost, you were charged" + " " + ChatColor.GREEN + preis);
    42. } else {
    43. if (sign.getLine(1).equalsIgnoreCase("Tent")){
    44. Generate.Tent(player);
    45. block.setType(Material.AIR);
    46. player.sendMessage(ChatColor.GOLD + "[Stronghold2]" + ChatColor.RED + "Poof.. You created an Tent, you were charged" + " " + ChatColor.GREEN + preis);
    47. }
    48. }
    49. }
    50. } else { player.sendMessage(ChatColor.RED + Main.config.getString("Stronghold2.Economy.NotEnoughMoney"));
    51. }
    52. }
    53. }
    54. }
    55. }
    56. }
    57.  


    EnderPearlListener
    Code:java
    1.  
    2. package com.arnie.sh2.Listeners;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12.  
    13.  
    14. public class EnderPearls implements Listener{
    15.  
    16. @EventHandler(priority=EventPriority.NORMAL)
    17. public void EnderPearls(PlayerInteractEvent event){
    18.  
    19. Player player = event.getPlayer();
    20. Action action = event.getAction();
    21.  
    22. if (player.getItemInHand().getType() == Material.ENDER_PEARL){
    23. if (action == Action.RIGHT_CLICK_AIR && action == Action.RIGHT_CLICK_BLOCK){
    24.  
    25. player.getItemInHand().setType(Material.GOLD_INGOT);
    26. player.sendMessage(ChatColor.GOLD + "[Stronghold2]" + " " + ChatColor.RED + "You are not allowed to use Ender Pearls so heres a Gold Ingot/s!");
    27. }
    28. }
    29. }
    30. }
    31.  


    And my ERROR
    Code:
     [SEVERE] Could not pass event PlayerInteractEvent to Stronghold2
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:303)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:459)
    at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:177)
    at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:147)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:605)
    at net.minecraft.server.Packet15Place.handle(SourceFile:39)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
    at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:551)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:449)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
    at com.arnie.sh2.Listeners.SignGeneratorListener.OutpostClick(SignGeneratorListener.java:25)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301)
    ... 12 more
    
     
  2. Ehh... I don't really understand the title, where do you see a casting problem ?

    But anyway, as the error suggets...
    null at line 25... but I belive your posted code has an extra line because the 25th line there points to a code that won't trigger a NPE... however, the next line:
    Code:
    Material blocktype = block.getType();
    Can trigger the NPE, because you didn't check if block is not null or if action is click on block.
     
Thread Status:
Not open for further replies.

Share This Page