Vault API NULL problem

Discussion in 'Spigot Plugin Development' started by ZebraBro, Aug 16, 2018.

  1. Hello,
    I'm trying to create scoreboard that takes details from vault api (money).

    The Code:

    Code (Java):
    private static Economy econ=null;
     

        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);

            getConfig().options().copyDefaults(true);
            saveConfig();
       
            if(setupEconomy())
                Log.info("Economy is enabled");
            else
                Bukkit.shutdown();
           

             
        }
     
     
     
    SETUP THE API:
     
         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;
            }


    The Command I have been trying to test the economy API:

    if(label.equalsIgnoreCase("econ")){
                    p.sendMessage(econ.getBalance(p.getName()));
    }

    But in game when I tried to process the command /econ and get my balance (from essentials balance)
    I got this exception:

    Code (Text):
    [13:03:58 INFO]: SlayZBro issued server command: /econ
    [13:03:58 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'econ' in plugin Dany1 v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_172]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_172]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_172]
    Caused by: java.lang.NullPointerException
        at me.Dany.project.Main.onCommand(Main.java:182) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
        ... 15 more

    It says null player as I understand it but I dont know how to fix this
    Please help.
     
  2. Optic_Fusion1

    Resource Staff

    This is not a resource so remove the resource tag
     
  3. Can you tell us what is Main.java, line 182?
     
  4. p.sendMessage(econ.getBalance(p.getName()));
     
  5. So either p is null, or econ. Figure out which one it is, then go from there. If we saw the whole onCommand method, we could probably pinpoint it.
     
  6. That isn't meter. That is the command that cause the problem I dont know how to fix the null bug
     
  7. Please post the whole onCommand() method, or at least up until line 182. The bug is bound to be in there.
     
  8. Code (Java):
    package me.Dany.project;

    import java.text.SimpleDateFormat;
    import java.util.Date;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Chunk;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.block.BlockState;
    import org.bukkit.block.Sign;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.libs.jline.internal.Log;
    import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
    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.SignChangeEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerChangedWorldEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;

    import org.bukkit.scoreboard.ScoreboardManager;



    import net.minecraft.server.v1_12_R1.PacketPlayOutChat;
    import net.minecraft.server.v1_12_R1.PlayerConnection;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import net.minecraft.server.v1_12_R1.IChatBaseComponent.ChatSerializer;

    public class Main extends JavaPlugin implements Listener {
        private static Economy econ=null;
       

        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);

            getConfig().options().copyDefaults(true);
            saveConfig();
            runnable();
            String x = getConfig().getString("dis");
            if(x.equalsIgnoreCase("true")) {runnable1();}
            if(setupEconomy())
                Log.info("Economy is enabled");
            else
                Bukkit.shutdown();
             

               
        }
       
       
       

       
         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;
            }

       
        @EventHandler
        public void PlayerJoin(PlayerJoinEvent e) {
       
            if(!e.getPlayer().hasPlayedBefore()) {
               
                double x = getConfig().getDouble("spawn.x");
                double y = getConfig().getDouble("spawn.y");
                double z = getConfig().getDouble("spawn.z");
               
               
                World w = Bukkit.getWorld("FuncionsWorld");
                e.getPlayer().teleport(new Location(w, x, y, z));
                e.setJoinMessage(ChatColor.GOLD+"New player joined to To MalibuCraft![Beta], Welcome!"+ChatColor.AQUA+e.getPlayer().getName() );
            }else {
           
            e.setJoinMessage(ChatColor.GOLD+"Welcome "+ChatColor.AQUA+e.getPlayer().getName() +ChatColor.GOLD+" To MalibuCraft![Beta]");
           
            }
            setScoreBoard(e.getPlayer());
        }
       
        @EventHandler
        public void movement(PlayerMoveEvent e) {
             if(e.getPlayer().getLocation().getWorld().getName().equalsIgnoreCase("world")) {
                 if(e.getPlayer().hasPermission("nte.Builder")) return;else
                e.getPlayer().setGameMode(GameMode.ADVENTURE);
            }
            else {
                if(e.getPlayer().hasPermission("nte.Builder")) return;else
                e.getPlayer().setGameMode(GameMode.SURVIVAL);
            }
        }
       

       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        Player p = (Player)sender;
       

           
            if(label.equalsIgnoreCase("setspawn")) {
                if(sender.hasPermission("dany.spawn")) {
                    double x = p.getLocation().getX();
                    double y = p.getLocation().getY();
                    double z = p.getLocation().getZ();
                    getConfig().set("spawn.x", x);
                    getConfig().set("spawn.y", y);
                    getConfig().set("spawn.z", z);
                    saveConfig();
                    p.sendMessage("Done.");
                }
                else {
                    sender.sendMessage("Sorry, you have no permission");
                }
            }
            if(label.equalsIgnoreCase("setspawnf")) {
                if(p.hasPermission("Dany.Fan")) {  
                   
                    double x = p.getLocation().getX();
                    double y = p.getLocation().getY();
                    double z = p.getLocation().getZ();
                    String world = p.getWorld().getName();
                    getConfig().set(world+".spawn.x", x);
                    getConfig().set(world+".spawn.y", y);
                    getConfig().set(world+".spawn.z", z);
                saveConfig();
                }
            }
            if(label.equalsIgnoreCase("spawnf")) {
                if(getConfig().contains("FuncionsWorld")) {
                    double x = getConfig().getDouble("FuncionsWorld.spawn.x");
                    double y = getConfig().getDouble("FuncionsWorld.spawn.y");
                    double z = getConfig().getDouble("FuncionsWorld.spawn.z");
                    World world = Bukkit.getWorld("FuncionsWorld");
                    p.sendMessage(ChatColor.YELLOW+"[Malibu] "+ChatColor.GREEN+"You have been teleported to "+ChatColor.AQUA+"Fanctions Spawn");
                    p.teleport(new Location(world, x, y, z));
                }else {
                    p.sendMessage(ChatColor.YELLOW+"[Malibu] "+ChatColor.RED+"No spawn yet");
                }
            }
           
           
           
           
            if(label.equalsIgnoreCase("econ")){
                    if(getConfig().contains("spawn")) {
               
                    double x = getConfig().getDouble("spawn.x");
                    double y = getConfig().getDouble("spawn.y");
                    double z = getConfig().getDouble("spawn.z");
                   
                    p.teleport(new Location(Bukkit.getServer().getWorld("FuncionsWorld"), x, y, z));
                   
                    p.sendMessage(econ.getBalance(p.getName()));
                    }
                    else {
                        p.sendMessage("No spawn yet");
                    }
                   
            }
     
  9. So, is econ null? Test for it!
     
  10. I'm just confused because
    Code (Text):
    p.sendMessage(econ.getBalance(p.getName()));
    should never have compiled in the first place because Player#sendMessage doesn't accept a double as its sole argument, yet vault Economy#getBalance returns just that, a double. I tested that with Spigot 1.12. Plus, in what you pasted, the code you said caused the bug isn't even in line 182, but 183. It's almost as if the code you posted is newer than whatever is running on your server, causing the exception. Anyway:
    • p cannot be null or there would have been a ClassCastException above.
    • econ cannot be null or the server would have shut down already.
    So I'm at a loss. Perhaps somebody else is able to help.
     
  11. Yes I tested it with if(econ=null) p.sendmessage("test"):
    and it worked

     
  12. Look my massage above i tested out with if(econ==null) p.sendmessage("test"): and that worked that means econ is null but how the sever should be shutted down what a bug
     
  13. any one can help?
     
  14. As player messages are through strings try to convert sending the balance to a string, and do you have another plugin that handles the economy? as vault alone can't handle someone's balance.
     
  15. As I already said above the problem is that econ =null I already tested it with if(econ==null) p.sendMessage("test"); and It worked