Problem/Bug Economy Problem

Discussion in 'Bukkit Help' started by MrWaffleman, Jul 30, 2015.

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


    So I've been developing a plugin related to Economy. I figured that depositPlayer() was deprecated, I tried switching to UUIDs, but it still didn't work. My code is shown below... everything works except for the depositPlayer() bit of code. I checked the console and the error is pointing to line 34, which is depositPlayer(), is there a new way to code this?

    package me.mrwaffleman;
    import java.util.ArrayList;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.Plugin;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    public class Sponge implements Listener {
        ArrayList<String> cooldown = new ArrayList<String>();
        public static Economy econ = null;
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerInteract(PlayerInteractEvent e) {
            if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            if(e.getClickedBlock().getType() == Material.SPONGE) {
                String pu = e.getPlayer().getUniqueId().toString();
                Player p = e.getPlayer();
                if(cooldown.contains(p.getName())) {
                    p.sendMessage(ChatColor.RED + "You have already robbed a store in the last 30 minutes!");
                p.sendMessage(ChatColor.DARK_GREEN + "+$1,000");
                EconomyResponse r = econ.depositPlayer(pu, 1000.00);
                if(r.transactionSuccess()) {
                else {
                    p.sendMessage(ChatColor.RED + "An error has occured, please contact a Staff Member!");
                double x = p.getLocation().getX();
                double z = p.getLocation().getZ();
                Bukkit.getServer().broadcastMessage("&f[&c&lDKMC&f] &e " + p.getName() + " Has robbed a store! At X: " + x + " Z: " + z);
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask((Plugin) new Sponge(), new Runnable() {
                    public void run() {
                }, 20 * 60 * 30);
    I may have just found the solution...

    EconomyResponse r = Main.econ.depositPlayer(player.getName(), <number>);
    And now the cooldown doesn't work... EDIT: And even more errors are happening as well... pointing to the same line.

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


    And those types of errors would be what? The error message contains the helpful information about type, not just lines. Most of the time, its easy to assume its a null pointer exception, but there could be any of dozens of other errors thrown up, and not knowing what type of error to account for, working with extremely limited segments of code.. its not possible.
    I'd put money on econ being null without any information further to go on
  3. Offline


    depositPlayer() is deprecated, I've been trying to find a solution or the replacement for it.
  4. Offline


    depricated isn't an error - its a warning to plan differently for the future, but not an error. You can add compiler ignore-warning for that. You use the solution that works, and if thats the only working solution...

    It would have been marked as depricated due to the namechange meaning usernames are no longer unique identifiers, to get your attention to think that out. Yet i dont believe they accept and pass uuids, just players and account-names
  5. Offline


    I did ignore the deprecation, but when I ran the server it kept pointing the error to that line, also I even tried converting to UUIDs as shown above, not really sure what the problem is, or the replacement to depositPlayer().
  6. Offline


    and once again, what is the ERROR that is being pointed at that line. The error message tells what TYPE of error it is - Null Pointer, Array out of Bounds, Concurrent Modification, ClassNotFound, etc etc.. by knowing which type of error you have, you are able to work out the cause - the cause for an array out of bounds error is VERY different than the cause for a null pointer error, you then look through your code for what could possibly cause the out of bounds error to happen, and work backwards aand backwards until you discover 'ah, i need that index to be 0 to n-1, not 1 to n..." for example.

    In this case though, I'll beat you to the punch - you're going to say its a null pointer error.

    Because you have defined econ to be null upon the class definition, and at no point have you then reassigned this econ to anything other than null, so it is null by the time that that line is executed, and hence, deferenceing it to get a method out of it = econ.depositPlayer() can not be dereferenced to an existing method because the econ object is null

    So how to fix this you then ask.

    Look at the vault examples. In most cases, you check and create the econ object during the startup onEnable() routine, and expose that object. You then refer to that econ object in your other code classes, OR you pass that reference to your class constructors if you realy want to keep a local scope version of it.
    Last edited: Jul 31, 2015
  7. Offline


    I followed this tutorial, and tried...

    EconomyResponse r = econ.depositPlayer((Player) e.getPlayer(), 2200.00);
    Yet it still did not work...
  8. Offline


    You're missing the point

    econ object


    HAS TO EXIST WITHIN SCOPE of where it is being used

    You are not setting it to anything except for null. EconomyResponse r = econ.ANYTHING() will fail because econ is a null object when you hit that code

    Focus on the econ object, where it lives, where it gets passed. If it is initialized somewhere, but not passed to where you are going to use it, or not accessed from where it is in scope, then you aren't accessing the economy bridge object that carries out the transactions, you are instead accessing only null objects.
  9. Offline


    I already did...

    public static Economy econ;
    But it didn't fix a thing and the error is still pointing at the same line. EDIT: I changed it to that code shown above, yet there is still a NullPointerException...
  10. Offline


    If you're referring to that public static Economy econ in the SAME CLASS code, then you are still missing the point.
    Backtrack the execution of your code. Simply declaring the object is not the same as assigning it the data to exist.

    Your class module above has an econ variable declared in it and set to null
    That "econ" WINS a constest for scope within that class, any use of econ will be the econ in the class, even if you have a public econ variable in every other class in your plugin. And if you do have a public econ in every other class, that still wont help you because you need to refer specifically to which one, or, get the object value from one of them.

    Since no where in your class upon creation, or through a setter function, are you giving life to the empty shell of econ that you have declared (except when you had it =null, which was explicitly assigned null value at declaration) then econ will be null.

    Null does not mean "I didn't declare this object". It means "I did not breathe life into this object and give it meaning"

    You have not given econ any meaning in this class. In some other class, perhaps, by using the vault example, you have declared and initialized an econ object such as your main class during the enabling.... but only that econ object in that class has meaning. If you have not initialized the econ object according to the examples in vault in anywhere in your plugin, then, you need to actually look at that example.

    Declaring is making a promise to do something with it, thats it. Your accession of econ in the line giving the errors is because you have only made a promise to have an economy object called econ, but you have not actually given life to the economy object as required.

    TLDR form: Assign some object data to your declared objects
    Player p = something or Player p ; p =something
    String s = something or String s; s=something
    World w = something or World w; w=something
    where is
    Economy econ = something or Economy econ; econ=something

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 11, 2016
Thread Status:
Not open for further replies.

Share This Page