Getting an error when my command is ran

Discussion in 'Spigot Plugin Development' started by asenthd, Jun 5, 2017.

  1. Hey guys, I'm working on a plugin. and everytime I try to run the command, it gives me an error. Code below
    Code (Text):
    Core (main class)

    package trollfaceryt.timedflygui;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.FileConfigurationOptions;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;

    import commands.TFlight;
    import net.milkbowl.vault.economy.Economy;


    @SuppressWarnings("unused")
    public class Core extends JavaPlugin {
        public static Economy econ = null;
     
        @Override
        public void onEnable() {
            File file = new File("config.yml");
            if(!file.exists()){
            saveDefaultConfig();}
            this.getCommand("tfly").setExecutor(new TFlight(this));
            setupEconomy();
            }

        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }

        private void registerConfig() {
        }

        public void onDisable() {
            reloadConfig();

        }
    }
    Code (Text):
    TFLIGHT (Command Class)

    package commands;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import net.md_5.bungee.api.ChatColor;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import trollfaceryt.timedflygui.Core;

    public class TFlight implements CommandExecutor, Listener {

        Core config;

        private static Economy econ = null;

        public TFlight(Core plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            config = plugin;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (label.equalsIgnoreCase("tfly"))
                if (args.length == 0 && sender instanceof Player) {
                    @SuppressWarnings("unused")
                    Player p = (Player) sender;
             
            @SuppressWarnings("deprecation")
            int balance = (int) econ.getBalance(sender.getName());
            if (balance < config.getConfig().getInt("price")) {
                Bukkit.getPlayer(sender.getName()).sendMessage(ChatColor.RED + "Insufficient funds!");
            }else{
            }
            @SuppressWarnings({ "deprecation", "unused" })
            EconomyResponse r = econ.withdrawPlayer(sender.getName(), config.getConfig().getInt("price"));
            if (args.length == 0 && sender instanceof Player) {
                Player p1 = (Player) sender;
                p1.setAllowFlight(true);
                sender.sendMessage(ChatColor.GREEN + "Flight has been purchased for" + config.getConfig().getInt(" price"));
            }
        }
            return true;
    }
    }
    Code (Text):
    Error:

    [22:22:55 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tfly' in plugin TimedFly v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
            at commands.TFlight.onCommand(TFlight.java:33) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
     
    • Funny Funny x 1
  2. You have an NullPointerException on line 33 in your command class.
    There are a few things I see which could throw that exception, so if you could show us which line is line 33 that would help us find our your issue faster.
     
  3. Code (Text):
            int balance = (int) econ.getBalance(sender.getName());
    THis is line 33
     
  4. use p.getName(); instead of sender.getName();
     
  5. Wait.... I unless I'm missing something, there is a couple things wrong with this line:
    Code (Java):
    private static Economy econ = null;
    I'm not sure why you need to instantiate that to null, and I can't find anywhere in your code where you in instantiate it so there's your issue.
     
  6. So I should change this line of code to
    Code (Text):
    public static Economy econ;
     
  7. It also looks like your missing a curly brace ( if (label.equalsIgnoreCase("tfly")) <----) i could be wrong though
     
  8. From what I see I'm not, but like I said up there is my new code correct? because it isnt working.
     
  9. Either econ or sender is null on line 33. Check using a debugger or println to see which it is.
     
  10. Strahan

    Benefactor

    Uhhh you do understand that the variable doesn't magically have data right? You have to do something with it besides declaring it.
     
  11. Code (Text):
    private boolean setupEconomy() {
            RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(Economy.class);
            if (economyProvider != null) {
                economy = economyProvider.getProvider();
            }
            return economy != null;
        }
    Try that instead, I've had issues with the way vault says to use their api. May or may not fix your issue.
     
  12. Like I stated previously, I am still very new to java and I'm currently practicing what I know. There is no reason to be rude just because you understand more about the language
     
  13. You have to give econ a value that isn't null. Also no point in making the variable static.
    Code (Text):
    public static Economy econ = null;
    This gives null value and is never changed before you access it, so it will throw a NullPointerException
    Code (Text):
    public static Economy econ;
    This gives it no value, hence when you try to access it, it will be null, and throw the same exception.
    Try this:

    Code (Text):
        private Economy economy;

        public boolean economyIsSetup() {
            RegisteredServiceProvider<Economy> economyProvider = Main.getServer().getServicesManager().getRegistration(Economy.class);
            if (economyProvider != null) {
                economy = economyProvider.getProvider();
            }
            return (economy != null);
        }
    Then before you access it in your code, always check to see if it is setup. If it isnt setup, then it should throw an NPE, which is what you want if it doesn't setup for whatever reason.
     
  14. Strahan

    Benefactor

    That's not "understanding more about the language", that's the freaking basics of programming. I'm not trying to be rude, but you should really not try to jump headfirst into plugin development if you don't have a basic grasp of programming. You should go learn Java first, then worry about making plugins.
     
    • Agree Agree x 1