Solved Problem with my configuration files plugin

Discussion in 'Spigot Plugin Development' started by Deitog, Jul 15, 2019.

Thread Status:
Not open for further replies.
  1. Hi. I'm doing a good plugin and there's a problem:
    This is the error argument:
    (The problem is in the "if")
    Code (Text):
    public static void registerPlayer(Player p,String pass1,String pass2) {
            ConfigManager configs = new ConfigManager(DiscordAppSimulator.getInstance());
            FileConfiguration reg = configs.getRegisters();
            String uuid = p.getUniqueId().toString();
            String name = p.getName();
            if(reg.contains("Registers."+uuid+"."+name)){
                Date now = new Date();
                SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                reg.set("Registers."+uuid+"."+name+".entry_date", "("+format.format(now)+")");
                reg.set("Registers."+uuid+"."+name+".password",pass1);
                configs.saveRegisters();
            }else{
                p.sendMessage(color(MessageUtil.alreadyRegistrered()));
            }
    The "if" worked for me before with the "contains" and now it does not
    And the reg.contains() worked for me and now, will it be that I have a problem? there is no plugin that interferes with it, why is this happening to me? Is it a spigotmc problem?
     
  2. Did you get any stack trace in the console, if you're so certain the error is on the line of the if?
     
  3. The error falls on the line where the "if" is
    upload_2019-7-15_13-15-54.png
    In the 54
     
  4. @Deitog What's the error from the console? If there is none, is it a compile error?
     
    • Like Like x 1
  5. We need the text of the error...
     
  6. From the info you've given us, I'm assuming there's something wrong with ConfigManager.getRegisters(), probably that it's returning null. It'd be helpful it you sent the whole error so we can understand exactly what is wrong.
     
    • Agree Agree x 1
  7. Code (Text):
    [12:02:03] [Server thread/INFO]: AtogMC issued server command: /dregister hola hola
    [12:02:03] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'dregister' in plugin DiscordAppSimulator v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_51]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_51]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    Caused by: java.lang.NullPointerException
        at net.deitog.dcsp.plugin.config.ConfigManager.reloadRegisters(ConfigManager.java:80) ~[?:?]
        at net.deitog.dcsp.plugin.config.ConfigManager.getRegisters(ConfigManager.java:73) ~[?:?]
        at net.deitog.dcsp.plugin.util.API.registerPlayer(API.java:54) ~[?:?]
        at net.deitog.dcsp.plugin.cmds.RegisterDiscord.onCommand(RegisterDiscord.java:30) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
     
  8. No, it does not return to null if it is not that the code is wrong and I just realized that there was a missing "!" in the "if"
     
  9. @Deitog There's a null value in your
    Code (Java):
    reloadRegisters()
    method. That means that the error is in that method, so we need to see your code from there. In fact, your actual
    Code (Java):
    FileConfiguration reg;
    line is what's null, and applying a method from that variable while that variable is null will result in a NullPointerException.
     
    • Like Like x 1
  10. Code (Text):
    private DiscordAppSimulator pl;
       
        public ConfigManager(DiscordAppSimulator pl){
            this.pl = pl;
        }
       
        private FileConfiguration messages = null;
        private File messagesfile = null;
        private FileConfiguration registers = null;
        private File registersfile = null;
        private FileConfiguration data = null;
        private File datafile = null;
        private FileConfiguration server = null;
        private File serverfile = null;
       
        public FileConfiguration getMessages(){
            if(messages == null){
                reloadMessages();
               
            }
            return messages;
        }
       
        public void reloadMessages(){
            if(messages == null){
                messagesfile = new File(pl.getDataFolder(),"messages.yml");
            }
            messages = YamlConfiguration.loadConfiguration(messagesfile);
            Reader defConfigStream;
            try{
                defConfigStream = new InputStreamReader(pl.getResource("messages.yml"),"UTF8");
                if(defConfigStream != null){
                    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                    messages.setDefaults(defConfig);
                }
            }catch(UnsupportedEncodingException e){
                e.printStackTrace();
            }
        }
        public void saveMessages(){
            try{
                messages.save(messagesfile);
               
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        public void registerMessages(){
            messagesfile = new File(pl.getDataFolder(),"messages.yml");
            if(!messagesfile.exists()){
                getMessages().options().copyDefaults(true);
                saveMessages();
                }
        }
       
        public FileConfiguration getRegisters(){
            if(registers == null){
                reloadRegisters();
            }
            return registers;
        }
       
        public void reloadRegisters(){
            if(registers == null){
                registersfile = new File(pl.getDataFolder(),"registers.yml");
            }
            registers = YamlConfiguration.loadConfiguration(registersfile);
            Reader defConfigStream;
            try{
                defConfigStream = new InputStreamReader(pl.getResource("registers.yml"),"UTF8");
                if(defConfigStream != null){
                    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                    registers.setDefaults(defConfig);
                }
            }catch(UnsupportedEncodingException e){
                e.printStackTrace();
            }
        }
        public void saveRegisters(){
            try{
                registers.save(registersfile);
               
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        public void registerRegistersFile(){
            registersfile = new File(pl.getDataFolder(),"registers.yml");
            if(!registersfile.exists()){
                getRegisters().options().copyDefaults(true);
                saveRegisters();
            }
        }
       
        public FileConfiguration getPluginData(){
            if(data == null){
                reloadPluginData();
               
            }
            return data;
        }
       
        public void reloadPluginData(){
            if(data == null){
                datafile = new File(pl.getDataFolder(),"/data/plugin_data.yml");
            }
            data = YamlConfiguration.loadConfiguration(datafile);
            Reader defConfigStream;
            try{
                defConfigStream = new InputStreamReader(pl.getResource("plugin_data.yml"),"UTF8");
                if(defConfigStream != null){
                    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                    data.setDefaults(defConfig);
                }
            }catch(UnsupportedEncodingException e){
                e.printStackTrace();
            }
        }
        public void savePluginData(){
            try{
                data.save(datafile);
               
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        public void registerPluginData(){
            datafile = new File(pl.getDataFolder(),"/data/plugin_data.yml");
            if(!datafile.exists()){
                getPluginData().options().copyDefaults(true);
                savePluginData();
                }
        }
        public FileConfiguration getServers(){
            if(server == null){
                reloadServers();
               
            }
            return server;
        }
       
        public void reloadServers(){
            if(server == null){
                serverfile = new File(pl.getDataFolder(),"/data/servers.yml");
            }
            server = YamlConfiguration.loadConfiguration(serverfile);
            Reader defConfigStream;
            try{
                defConfigStream = new InputStreamReader(pl.getResource("servers.yml"),"UTF8");
                if(defConfigStream != null){
                    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                    server.setDefaults(defConfig);
                }
            }catch(UnsupportedEncodingException e){
                e.printStackTrace();
            }
        }
        public void saveServers(){
            try{
                server.save(serverfile);
               
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        public void registerServers(){
            serverfile = new File(pl.getDataFolder(),"/data/servers.yml");
            if(!serverfile.exists()){
                getServers().options().copyDefaults(true);
                saveServers();
                }
        }
       
        public void registerConfig(){
            File config = new File(pl.getDataFolder(),"config.yml");
            pl.rutaConfig = config.getPath();
                if(!config.exists()){
                    pl.getConfig().options().copyDefaults(true);
                    pl.saveConfig();
                }
            }
     
  11. @Deitog Could you give me the line numbers as well? That would help a great deal in figuring out exactly where your issue is.
     
  12. @Deitog It seems that the instance of your plugin, the line
    Code (Java):
    private DiscordAppSimulator pl;
    is null, meaning it doesn't have an existing value. Before the line
    Code (Java):
    registersfile = new File(pl.getDataFolder(),"registers.yml");
    insert the following, and let me know what prints to the console.
    Code (Java):
    System.out.println(pl == null); // returns whether the plugin is null
    System.out.println(pl.getDataFolder() == null); // returns whether the data folder is null
     
  13. @NickBurnett420
    upload_2019-7-15_15-53-55.png
    Where do I have to import it? in what class?
     
  14. @Deitog In your ConfigManager class, at the beginning of the reloadRegisters function.
     
  15. @NickBurnett420
    And what if I eliminate the constructor? and add this method:
    Code (Text):
    public void onEnable(DiscordAppSimulator plugin){
            this.pl = plugin;
            this.registerConfig();this.registerMessages();
            this.registerPluginData();this.registerRegistersFile();
            this.registerServers();
        }
    And the method I put in the main class:
    Code (Text):
           @Override
        public void onEnable(){
            ConfigManager configs = new ConfigManager();
            configs.onEnable(this);
        }
     
  16. @NickBurnett420
    And as you say it is equal to null with that method it is no longer
     
  17. @Deitog The plugin instance is no longer null with that method. Does that fix your other errors or are you still having trouble?
     
  18. @NickBurnett420
    I just tested if it solves the error, the "if" is fixed but the reloadRegisters() is not working
    upload_2019-7-15_17-9-32.png
    And it ends in the box that I'm marking.
    Code (Text):
    [16:08:40] [Server thread/INFO]: AtogMC issued server command: /dregister
    [16:08:44] [Server thread/INFO]: AtogMC issued server command: /dregister hola hola
    [16:08:44] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'dregister' in plugin DiscordAppSimulator v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_51]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_51]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    Caused by: java.lang.NullPointerException
        at net.deitog.dcsp.plugin.config.ConfigManager.reloadRegisters(ConfigManager.java:84) ~[?:?]
        at net.deitog.dcsp.plugin.config.ConfigManager.getRegisters(ConfigManager.java:77) ~[?:?]
        at net.deitog.dcsp.plugin.util.API.registerPlayer(API.java:51) ~[?:?]
        at net.deitog.dcsp.plugin.cmds.RegisterDiscord.onCommand(RegisterDiscord.java:30) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
    upload_2019-7-15_17-13-59.png
     
  19. @Deitog Again, check if the plugin instance you're using is null or not. If it is not null (it will be), then it must be in the getDataFolder function (probably not).
     
Thread Status:
Not open for further replies.