Solved Custom Config Null Error

Discussion in 'Spigot Plugin Development' started by DatSquidFTW, Jan 24, 2020.

  1. Code (Java):
    public class Data {

        private File file;
        private FileConfiguration trophies;

        public void setupFile(){

            file = new File(Bukkit.getServer().getPluginManager().getPlugin("Trophies").getDataFolder(), "data.yml");

            if (!(file.exists())){
                try {
                    boolean success = file.createNewFile();
                    System.out.println("[Trophies] Data.yml creation: " + success);
                } catch (IOException e) {
                    System.out.println("[Trophies] Data.yml creation failed");
                }
            }
            trophies = YamlConfiguration.loadConfiguration(file);
        }

        public FileConfiguration getFile(){
            return trophies;
        }

        public void reloadFile(){
            saveFile();
            if (file != null){
                trophies = YamlConfiguration.loadConfiguration(file);
            } else {
                System.out.println("[Trophies] File is returning null (Reload Method: reload.yml");
            }
        }

        public void saveFile() {
            try {
                if (file != null){
                    trophies.save(file);
                } else {
                    System.out.println("[Trophies] File is returning null (Save Method: data.yml");
                }
            } catch (IOException e) {
                System.out.println("[Trophies] Data.yml save has failed");
            }
        }

        public void loadDefaults(){
            getFile().addDefault("Test", "Bruh moment");
        }

    }
    Here is my code for creating a custom file. It works great except for I need to reload it. The file variable is returning null. The file is created successfully and in the right folder. However, it returns null whenever I try and reload it using the method above.
    Am I doing anything wrong here?
     
  2. Can you post the exact output of your console?
     
  3. Hello there :)

    I use this to reload my customconfig!
    Code (Java):
    public static void reloadCustomConfig() {
            try {
                fileconfig.load(file);
            } catch (IOException | InvalidConfigurationException e) {
                Bukkit.getConsoleSender().sendMessage("§c[Config] Your config is incorrect!");
                e.printStackTrace();
            }
           
        }
     
  4. From a quick look, you are running the saveFile() method which needs an instance of your file, and it doesn't get passed.
    Also, why would you need to save your file? you only need:
    Code (Text):
    trophies = YamlConfiguration.loadConfiguration(file);
    In your Reload() method.
     
  5. I see, but my code works for me! :)
    Try my code, if you got error, please send me the error
     
  6. ???
    Your reason for posting is that is gives errors. so idk what to say honestly.
     
  7. Where is the error code? I don't see
     
  8. " It works great except for I need to reload it."
    "The file variable is returning null."

    Are you high? that's what you said.
    I meant null, doesn't really matter.
     
  9. Try my configmanager code, work perfectly for me!

    Code (Java):
    package com.fighter.Config;

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

    import org.bukkit.Bukkit;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.Listener;

    import com.fighter.Main.Main;

    public class ConfigManager implements Listener {

        private static File file;
        private static FileConfiguration fileconfig;
        private static Main plugin = Main.getPlugin(Main.class);

        public FileConfiguration getPlayerConfig() {
            return fileconfig;
        }

        public void saveCustomConfig() throws InvalidConfigurationException {
            try {
                fileconfig.save(file);
                Bukkit.getConsoleSender().sendMessage("Configuration has been saved!");
            } catch (IOException e) {
                Bukkit.getConsoleSender().sendMessage("We can't reload your configuration!");
                e.printStackTrace();
            }
        }

        public static void createPlayerConfig() {

            file = new File(plugin.getDataFolder(), "players.yml");

            if (!file.exists()) {
                try {

                    file.getParentFile().mkdir();
                    plugin.saveResource("players.yml", false);

                } catch (Exception e) {
                    Bukkit.getConsoleSender().sendMessage("");
                    e.printStackTrace();
                }
            }

            fileconfig = new YamlConfiguration();
            try {
                fileconfig.load(file);
            } catch (IOException | InvalidConfigurationException e) {
                Bukkit.getConsoleSender().sendMessage("Error in trying to load the configuration!");
                e.printStackTrace();
            }

        }
        public static void reloadCustomConfig() {
            try {
                fileconfig.load(file);
            } catch (IOException | InvalidConfigurationException e) {
                Bukkit.getConsoleSender().sendMessage("§c[Config] Your config is incorrect!");
                e.printStackTrace();
            }
           
        }

    }
     
     
  10. Anyways a way I think will work is making the setupFile() method static, pretty sure it'll make the File variable not null, and making it static
    haha, I was about to write that the solution is to make the setupFile() method static and then saw you posted this, look at your createPlayerConfig() method, it's static, you probably missed it accidentally, happens to everyone :)
    EDIT: make File static
     
  11. You aren't talking with OP rn :)
     
  12. Thank you! That fixed it