Solved Getting a "null" error

Discussion in 'Spigot Plugin Development' started by SpiroMarshes, May 31, 2016.

  1. Trying to get the balance of a player using vault API,

    Here is my error:
    Code (Text):
    [19:58:32] [Server thread/INFO]: SpiroMarshes issued server command: /blackjack 15 20
    [19:58:32] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'blackjack' in plugin MystiqueCore v1.1.2
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_91]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_91]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
        at com.spiromarshes.MystiqueCore.BlackJack.onCommand(BlackJack.java:55) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
        ... 15 more
    Here is my code:
    Code (Text):
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
        {
           
            if (!(sender instanceof Player))
            {
                sender.sendMessage("You must be a player to use this command!");
                return false;
            }
           
            Player player = (Player) sender;
            OfflinePlayer offlinePlayer = (OfflinePlayer) player;
           
            if(command.getName().equalsIgnoreCase("blackjack") && args.length > 0 && player.hasPermission("mystique.blackjack"))
            {
                double bet = Double.parseDouble(args[0]);
                double stand = Double.parseDouble(args[1]);
                Boolean win = false;
               
                if(bet > economy.getBalance(offlinePlayer))
                {
                    player.sendMessage("ERROR: You can't bet an amount greater than what you currently have!");
                    return true;
                }
     
  2. Economy is probably null.
     
    • Agree Agree x 1
  3. My constructor:

    Code (Text):

        private Economy economy;
        private MystiqueMain = plugin;

        public BlackJack(MystiqueMain pl, Economy econ)
        {
            plugin = pl;
            economy = econ;
        }
    I don't think it's null. I'm pretty sure it's something to do with when I try to getBalance
     
  4. Player extends OfflinePlayer so you can get rid of offlinePlayer and just pass player to the economy method.
     
  5. I did that prior to testing it with the whole OfflinePlayer garbage, same thing.
     
  6. Yeah, just thought I would mention it.
    As far as your problem goes what is the code at line 55
     
  7. if(bet > economy.getBalance(offlinePlayer))

    That's what made me try it with the OfflinePlayer.
     
  8. Ensure econ is not null, and you can use this method instead if you choose "has(OfflinePlayer player, double amount)" this will tell you if the player has that amount of money.
     
  9. I'm gonna PM you.
     
  10. @Redrield was right, your economy is most likely null. The code you just showed doesn't actually prove much as you can still pass a null reference into the constructor. You should keep working backwards and find where you actually get the Economy instance.
     
    • Agree Agree x 1
  11. Here is my main class:

    Code (Text):
    package com.spiromarshes.MystiqueCore;

    import java.util.logging.Logger;

    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.milkbowl.vault.Vault;
    import net.milkbowl.vault.economy.Economy;

    public class MystiqueMain extends JavaPlugin{
       
        private Vault vault;
        private Economy economy;
       
        public void onEnable(){
           
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
           
            logger.info(pdfFile.getName() + " has been enabled (V." + pdfFile.getVersion() + ")");
           
            registerConfig();
            registerEvents();
            registerCommands();
           
            this.vault = (Vault) this.getServer().getPluginManager().getPlugin("Vault");
            if (this.vault != null) {
                RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
                if (rsp != null) {
                    economy = rsp.getProvider();
                    this.getLogger().info("[Debug] Economy Enabled.");
                } else if (rsp == null) {
                    this.getLogger().info("[Debug] No economy plugin detected.");
                }
            }      

        }
       
        public void onDisable(){
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
           
            logger.info(pdfFile.getName() + " has been disabled (V." + pdfFile.getVersion() + ")");
            logger.info("Thanxxxxxxx K Bai");
        }
       
        private void registerConfig()
        {
            getConfig().options().copyDefaults(true);
            saveConfig();
           
        }
       
        private void registerEvents()
        {
            PluginManager pm = getServer().getPluginManager();
           
            pm.registerEvents(new PlayerWelcomer(this), this);
           
        }
       
        private void registerCommands()
        {
            getCommand("blackjack").setExecutor(new BlackJack(this, economy));
           
        }
    }
     
     
  12. I'm still trying to learn the Spigot API and the is the first time that I'm working with vault. If I'm missing something extremely obvious, I apologize.
     
  13. And there we have it! Economy is null!

    Code (Text):
    private Economy economy;
    remains null until you call
    Code (Text):
    economy = rsp.getProvider();
    But you pass the reference into the BlackJack constructor before that when you call
    Code (Text):
    registerCommands();
     
    • Agree Agree x 1
  14. I just noticed it as well. I feel slightly stupid now.
     
  15. Call the registerCommands() method after you initialize your economy instance.

    EDIT: Ninja'd
     
    • Friendly Friendly x 1