Solved Checking player balance using args?

Discussion in 'Spigot Plugin Development' started by CameronVezina, Jan 6, 2020.

Thread Status:
Not open for further replies.
  1. Code (Java):
    if (econ.getBalance(p) <= Double.parseDouble(args[0])) {
                                    p.sendMessage("§c- §c§l$" + args[0]);
                                    return true;
                                } else {
                                    p.sendMessage("§c§l(!) §cYou do not have enough to withdraw that much!");
                                }
    ^ Is giving me a null response in console, I tried multiple ways of checking if their balance is greater than or equal to the args they specified but none have worked. Any ideas?
     
  2. what do you mean by null response? The NPE? If so, econ is probably null.
     
  3. also your logical statement is backwards. "if the player has less than or equal the amount"
     
    • Agree Agree x 1
  4. When I type the command, I get a null response, "[18:33:22 ERROR]: null"

    Oh yeah, I changed it and it doesnt fix it
     
  5. Which specific line is printing "null"? Are you sure that's the only error you see?
    Try splitting the lines like underneath, then run the code:
    Code (Java):
            if (econ
                    .getBalance
                    (p) <=
                    Double.parseDouble(
                            args[0]))
     
  6. what good does that do? if theres an NPE, econ is the only thing that can throw an NPE
     
  7. Line 42, which is ".getBalance"
     
  8. To me, it seems like the OP doesn't understand why that's the case. This would be a good chance to teach them how to look for such issues, whether it's with 1, 2 or 5 variables in a line.

    I assume there's no balance registered / saved for said player (p) then. Could you write an if check like so?:
    Code (Java):
    if (econ.getBalance(p) == null) {
        // Print something
    }
     
  9. "The operator == is undefined for the argument type(s) double, null"
     
  10. Right, my bad.
    Is this hooked with Vault or your own economy system? Either way, you want to use a method which checks whether or not the player's registered in the economy system.
     
  11. I'm using Vault & Essentials, not sure how to check their balance
     
    #11 CameronVezina, Jan 7, 2020
    Last edited: Jan 7, 2020
  12. Essentials should create the user by default then, as long as the account has logged in at least once.
    Judging by what I see here, I'd try hooking into Essentials' API and use the Economy module from there rather than hooking into Vault, hopefully that'll work.
    Try to use this method to check whether or not the user already exists.

    Vault is only an API, and as Essentials is the one that manages it, I'd recommend hooking into that instead, as you said you use both in combination.
     
  13. https://github.com/MilkBowl/VaultAPI - you can check here for how to use the vault api. You essentially need to check if the player specified in the arguments is online, and if they are query their balance using the methods outlined here.
     
    • Like Like x 1
  14. econ is null. lets see the whole class

    /e oh interesting. not an npe. thats a weird response to what youre doing
    /e2 ok now im confused. we're talking line numbers like we have a stacktrace. is there a full length stacktrace or not? if there is, can you post the full stacktrace?
     
    #14 Warren1001, Jan 7, 2020
    Last edited: Jan 7, 2020
  15. bro that's not the full stacktrace

    yeah, op didn't post the full stacktrace
     
  16. Clearly either the reference econ or p holds null, and when you try to access it's method it throws the NullPointerException.

    Try using this and see what happens:
    Code (Java):
        // Parse the argument.
        double argumentNumber = 0;
        boolean correctFormat;
        try {
            argumentNumber = Double.parseDouble(args[0]);
            correctFormat = true;
        } catch (NumberFormatException e) {
            argumentNumber = 0;
            correctFormat = false;
        }

        // Tell the player they typed rubbish.
        if (!correctFormat) {
            p.sendMessage("Invalid number format.");
            return false;
        }

        // Test which value might be null to debug your problem.
        if (econ == null)
            throw new NullPointerException("econ is null");
        if (p == null)
            throw new NullPointerException("p is null");

        // Check balance.
        if (correctFormat && econ.getBalance(p) <= argumentNumber)) {
            p.sendMessage("§c- §c§l$" + args[0]);
            return true;
        } else if (correctFormat) {
            p.sendMessage("§c§l(!) §cYou do not have enough to withdraw that much!");
            return true;
        }

     
     
  17. Caused by: java.lang.NullPointerException: econ is null

    I guess i'm not registering the economy right?



    Command class:
    https://pastebin.com/NJuFJ1iV

    Main class:
    https://pastebin.com/puH8A9af
     
    • Agree Agree x 1
Thread Status:
Not open for further replies.