Vault EconomyResponse NPE

Discussion in 'Plugin Development' started by 1SmallVille1, Mar 9, 2013.

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

    1SmallVille1

    Hi, so I'm working on a plugin that will reward a player a certain amount of money for killing someone, but I'm receiving trouble when calling on the EconomyResponse and withdraw/deposit methods. I get an NPE in the console. This is my code:
    Code:
    public static Economy econ;
     
    public void onPlayerDeath(PlayerDeathEvent evt) {                       
     
    EconomyResponse er = econ.withdrawPlayer(
                                    chos.getName(), amount);
                            EconomyResponse wp = econ.depositPlayer(challenger,
                                    amount);
     
                            if (er.transactionSuccess()) {
                                chos.sendMessage(ChatColor.GRAY
                                        + "["
                                        + ChatColor.AQUA
                                        + "PvPArenas"
                                        + ChatColor.GRAY
                                        + "]: "
                                        + ChatColor.GOLD
                                        + "$"
                                        + amount
                                        + ChatColor.RED
                                        + " has been subtracted from your account for losing");
                            }
     
                            if (wp.transactionSuccess()) {
                                chal.sendMessage(ChatColor.GRAY
                                        + "["
                                        + ChatColor.AQUA
                                        + "PvPArenas"
                                        + ChatColor.GRAY
                                        + "]: "
                                        + ChatColor.BLUE
                                        + "$"
                                        + amount
                                        + ChatColor.GOLD
                                        + " has been added to your account for winning!");
    then this is my main class:
    Code:
    public static Economy econ;   
     
    public void onEnable(){
           
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(pl, this);
           
            setupEconomy();
    my plugin.yml:
    Code:
    name: PvP
    version: 0.3
    main: com.yahoo.s11tomp.PvPTest.PvP
    depend: [Vault]
    and finally, the error:
    Code:
    21:12:40 [SEVERE] Could not pass event PlayerDeathEvent to PvP v0.3
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.v1_4_R1.event.CraftEventFactory.callPlayerDeat
    hEvent(CraftEventFactory.java:332)
            at net.minecraft.server.v1_4_R1.EntityPlayer.die(EntityPlayer.java:256)
            at net.minecraft.server.v1_4_R1.EntityLiving.damageEntity(EntityLiving.j
    ava:758)
            at net.minecraft.server.v1_4_R1.EntityHuman.damageEntity(EntityHuman.jav
    a:616)
            at net.minecraft.server.v1_4_R1.EntityPlayer.damageEntity(EntityPlayer.j
    ava:309)
            at net.minecraft.server.v1_4_R1.Entity.A(Entity.java:377)
            at net.minecraft.server.v1_4_R1.Entity.y(Entity.java:341)
            at net.minecraft.server.v1_4_R1.EntityLiving.y(EntityLiving.java:310)
            at net.minecraft.server.v1_4_R1.Entity.j_(Entity.java:241)
            at net.minecraft.server.v1_4_R1.EntityLiving.j_(EntityLiving.java:508)
            at net.minecraft.server.v1_4_R1.EntityHuman.j_(EntityHuman.java:155)
            at net.minecraft.server.v1_4_R1.EntityPlayer.g(EntityPlayer.java:194)
            at net.minecraft.server.v1_4_R1.PlayerConnection.a(PlayerConnection.java
    :352)
            at net.minecraft.server.v1_4_R1.Packet10Flying.handle(SourceFile:136)
            at net.minecraft.server.v1_4_R1.NetworkManager.b(NetworkManager.java:290
    )
            at net.minecraft.server.v1_4_R1.PlayerConnection.d(PlayerConnection.java
    :113)
            at net.minecraft.server.v1_4_R1.ServerConnection.b(SourceFile:39)
            at net.minecraft.server.v1_4_R1.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_4_R1.MinecraftServer.r(MinecraftServer.java:5
    98)
            at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:2
    24)
            at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:4
    94)
            at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java
    :427)
            at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:8
    49)
    Caused by: java.lang.NullPointerException
            at com.yahoo.s11tomp.PvPTest.PvPListener.onPlayerDeath(PvPListener.java:
    53)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.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.ja
    va:425)
            ... 26 more
     
  2. Offline

    GodzOfMadness

  3. Offline

    1SmallVille1

    it's a part of the EconomyResponse variable setting:

    chos.getName(), amount
     
  4. Offline

    GodzOfMadness

    1SmallVille1 where are you getting the variable "chos" from?
     
  5. Offline

    1SmallVille1

    sorry I didn't include that part:
    Code:
    String chosen = Commands.chosen;
            String challenger = Commands.challenger;
     
            double amount = Commands.amount;
     
            if (chosen != null) {
                if (challenger != null) {
                    if (evt.getEntity().getName().equalsIgnoreCase(chosen)) {
     
                        Player chos = Bukkit.getServer().getPlayer(chosen);
                        Player chal = Bukkit.getServer().getPlayer(challenger);
     
  6. Offline

    GodzOfMadness

    1SmallVille1 do some checks try and broadcast or log (doesn't matter) the string and the ammount and if you get null or an error try and initialize Commands by doing public static Commands commands;
    and then make a constructor
    public CLASSNAME(Commands c){
    commands = c;
    }
    then use the c variable
     
  7. Offline

    1SmallVille1

    I did this but neither are null or errors
     
  8. Offline

    GodzOfMadness

    1SmallVille1 i noticed you never set "econ" to anything so it could be causing a npe but if not the case then
    did you try and broadcast player chos name?
     
  9. Offline

    1SmallVille1

    sorry, what would I set econ to? And I did broadcast chos name, it was the player's name, not null
     
  10. Offline

    GodzOfMadness

    1SmallVille1 looking on Vault's page it tells you how to hook into their economy as so
    private boolean setupEconomy()
    {
    RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    if (economyProvider != null) {
    economy = economyProvider.getProvider();
    }

    return (economy != null);
    }

    use this method with your code sense it is a boolean you could make this check first.
     
  11. Offline

    1SmallVille1

    yeah, I already had this in beneath my onEnable()
    Code:
        private boolean setupEconomy() {
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager()
                    .getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = (Economy) rsp.getProvider();
            return econ != null;
        }
    any idea what's throwing the NPE?
     
  12. Offline

    GodzOfMadness

    is your death event in your main class?
    and you should have this as a check for the death event
    so like
    if(setupEconomy){
    do stuff
    }else{
    economy is null
    }
     
  13. Offline

    1SmallVille1

    no the death event is in a separate class
     
  14. Offline

    GodzOfMadness

    1SmallVille1 then move the event to that class and have the Economy econ in the class also
    then when someone dies do
    if(setupEconomy){
    do your code
    }else
    economy is null
     
  15. Offline

    1SmallVille1

    Thank you so much! This fixed it!
     
Thread Status:
Not open for further replies.

Share This Page