NullPointerException with plugin with Implemented Vault

Discussion in 'Plugin Development' started by AlexHystra, Oct 30, 2014.

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

    AlexHystra

    Hello there I'm bassically trying to make plugin just which gives you money when you kill a monster pretty simple uhh ? But I just want to use Vault with this couse I'll probably make some othere stuffs with Vualt. So here is my code :

    Code:java
    1. package me.Al3xFx;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4. import net.milkbowl.vault.economy.EconomyResponse;
    5. import net.milkbowl.vault.permission.Permission;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityDeathEvent;
    13. import org.bukkit.plugin.RegisteredServiceProvider;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16.  
    17. public class KillMoney extends JavaPlugin implements Listener{
    18. public static Economy econ = null;
    19. public static Permission perms = null;
    20.  
    21. public void onEnable(){
    22. getLogger().info("Plugin has been enabled");
    23. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    24. }
    25.  
    26. public void onDisable(){
    27. }
    28.  
    29.  
    30. private boolean setupPermissions() {
    31. RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
    32. perms = rsp.getProvider();
    33. return perms != null;
    34. }
    35.  
    36.  
    37.  
    38. private boolean setupEconomy() {
    39. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    40. return false;
    41. }
    42. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    43. if (rsp == null) {
    44. return false;
    45. }
    46. econ = rsp.getProvider();
    47. return econ != null;
    48. }
    49.  
    50.  
    51.  
    52. @EventHandler
    53. public void onEntityDeath(EntityDeathEvent e ){
    54. Player p = e.getEntity().getKiller();
    55.  
    56. if(!(e.getEntity().getKiller() instanceof Player)){
    57. p.sendMessage("fuck off");
    58. }
    59. else{
    60.  
    61.  
    62. p.sendMessage(ChatColor.AQUA + "You Killed a: " + ChatColor.GREEN + e.getEntity().getType());
    63. EconomyResponse r = econ.depositPlayer(p.getName(), 10.00);
    64. if(r.transactionSuccess()) {
    65. p.sendMessage(ChatColor.GREEN + "You have been awaraded with $10.00 for killing" + e.getEntity().getType());
    66. } else {
    67. p.sendMessage(ChatColor.DARK_RED + "ECONOMY" + ChatColor.RED + " Is not working propiertly please contact an admin right away !");
    68. }
    69.  
    70. }
    71.  
    72.  
    73.  
    74. }
    75.  
    76. }
    77.  

    So i got Vault imported .
    My plugin.yml is fine .
    Where is my problem ?
    My log :
    Code:
    [18:33:42 ERROR]: Could not pass event EntityDeathEvent to AbsoluteZero v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks
    ]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks
    ]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks
    ]
            at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callEntityDeat
    hEvent(CraftEventFactory.java:353) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-
    R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.EntityLiving.die(EntityLiving.java:799)
    [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.EntityLiving.damageEntity(EntityLiving.j
    ava:734) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks
    ]
            at net.minecraft.server.v1_7_R3.EntityAnimal.damageEntity(SourceFile:145
    ) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.EntityHuman.attack(EntityHuman.java:925)
    [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :1106) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInUseEntity.a(SourceFile:55) [
    craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInUseEntity.handle(SourceFile:
    10) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Caused by: java.lang.NullPointerException
            at me.Al3xFx.KillMoney.onEntityDeath(KillMoney.java:65) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _20]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _20]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_20]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_20]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks
    ]
            ... 18 more
    >
     
  2. Offline

    Unica

    AlexHystra

    Your casting doesn't make sense. First check, then cast.
    Secondly, you're generating a nullPointer exception in 'onEntityDeath'. Im pretty sure that's because sometimes there killer is null.

    Code:java
    1. @EventHandler
    2. public void onEntityDeath(EntityDeathEvent e){
    3. if(e.getEntity().getKiller() instanceof Player){
    4. Player p = (Player) e.getEntity();
    5. p.sendMessage("You killed an "+e.getEntity().getType());
    6. }
    7. }


    Secondly, read your first check
    Code:java
    1. if(!(e.getEntity().getKiller() instanceof Player)){
    2. p.sendMessage("Swear Word");
    3. }


    You're basically doing this.
    If the entity is NOT killed by an player, send a message to that player.
     
  3. Offline

    fireblast709

    AlexHystra You do know that getKiller() either returns a Player or null, right? Check for null instead of instanceof.
    Unica whilst correct about the check-cast order, ^
     
  4. Offline

    Unica

    fireblast709
    What about a creeper / skeleton killing a entity :eek:?
     
  5. Offline

    fireblast709

    Unica the return type of the method is Player. Thus, if the player was killed by another entity, it will just be null (because that Player is nonexistent)
     
  6. Offline

    BetaNyan

    The killer is null.
     
  7. Offline

    AlexHystra

    Alright guys I've just did some null checks and the problem is not from the cast. The problem becomes from my :

    Code:java
    1. EconomyResponse r = econ.depositPlayer(p.getName(), 10.00);


    It's mentioned in the report.

    Actually i found out something else look it seems that the killer is null because if I use this :
    Code:java
    1. @EventHandler
    2. public void onEntityDeath(EntityDeathEvent e ){
    3. Player p = e.getEntity().getKiller();
    4.  
    5. if(!(e.getEntity().getKiller() != null)){
    6.  
    7.  
    8. p.sendMessage(ChatColor.AQUA + "You Killed a: " + ChatColor.GREEN + e.getEntity().getType());
    9. EconomyResponse r = econ.depositPlayer(p.getName(), 10.00);
    10. if(r.transactionSuccess()) {
    11. p.sendMessage(ChatColor.GREEN + "You have been awaraded with $10.00 for killing" + e.getEntity().getType());
    12. } else {
    13. p.sendMessage(ChatColor.DARK_RED + "ECONOMY" + ChatColor.RED + " Is not working propiertly please contact an admin right away !");
    14. }
    15.  
    16. }else{
    17. Bukkit.broadcastMessage("test");
    18. }
    19.  
    20.  
    21.  
    22. }


    The server returns test .
    So what should I do and where is my mistake ?

    fireblast709
    Also if I try to do something super simple like :
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args)
    2. {
    3. if(cmdLabel.equalsIgnoreCase("pari")){
    4. Player player = (Player)sender;
    5. econ.depositPlayer(player, 10);
    6. }
    7. return false;
    8.  
    9.  
    10.  
    11. }

    It again gives my NullPointerException on line
    Code:java
    1. econ.depositPlayer(player, 10);


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  8. Offline

    teej107

  9. Offline

    Rocoty

    AlexHystra As teej107 is trying to point out, you are never really setting up Economy or Permissions, and then you expect them to work...
     
  10. Offline

    fireblast709

    AlexHystra In addition to the post above:
    • use cmd.getName() instead of cmdLabel, for alias support.
    • Check if sender instanceof Player before casting to Player.
     
Thread Status:
Not open for further replies.

Share This Page