Solved Custom config not loading across classes

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

Thread Status:
Not open for further replies.
  1. Hi!

    So I'm following this example: https://www.spigotmc.org/wiki/config-files/#using-multiple-configuration-files

    My Main looks like this:
    Code (Text):
    public class Main extends JavaPlugin {

        public static Main instance;
        private File configf, spawnf;
        private FileConfiguration config, spawn;

        @Override
        public void onEnable() {
            instance = this;
            registerListeners();
            createConfigs();
        }
        /*
        @Override
        public void onDisable() {

        }
        */

        private void createConfigs() {

            configf = new File(getDataFolder(), "config.yml");
            spawnf = new File(getDataFolder(), "spawns.yml");

            if (!configf.exists()) {
                getLogger().log(Level.WARNING, ChatColor.RED + "No config file found! Creating.");
                configf.getParentFile().mkdirs();
                saveResource("config.yml", false);
            }
            if (!spawnf.exists()) {
                getLogger().log(Level.WARNING, ChatColor.RED + "No spawn file found! Creating.");
                spawnf.getParentFile().mkdirs();
                saveResource("spawns.yml", false);
            }

            config = new YamlConfiguration();
            spawn = new YamlConfiguration();
            try {
                config.load(configf);
                spawn.load(spawnf);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public FileConfiguration getSpawnList() {
            return this.spawn;
        }

        private void registerListeners() {
            PluginManager pm = Bukkit.getPluginManager();

            pm.registerEvents(new MyBidding(), this);
        }

    }
    When I ask for a value from the "spawns.yml" file it returns a value as it should.

    My class which I'm trying to get values to looks like this:
    Code (Text):
    public class MyBidding implements Listener {

        private FileConfiguration config = Main.instance.getConfig();
        private FileConfiguration spawn = Main.instance.getSpawnList();

    }
    When I try to pull a value from the spawns file it throws an NPE as if it hasn't loaded. Is there something that I need to add to perhaps my getSpawnList function?
     
  2. You may need to call createConfigs before you call registerListeners. As it stands it looks like MyBidding will try to access the spawn list before it is populated. I can't see how this would directly cause an NPE, but it definitely would later on if you tried to use MyBidding.spawn since it would be null.
     
    • Winner Winner x 1
  3. Addition to what he NathanWolf in his post. It throws an npe because the two variables are set when the instance is created (before the variables in the main class are set). So you copy the pointer to main and spawn (in the Main class) to the variables in the MyBidding class. They are both null so in the new class they will be null. After that you set the pointers in the Main class to the YamConfiguration instance. I hope you understand it.

    Quick explanation:
    Main pointers & MyBidding pointers -> null
    MyBidding pointer -> Main pointers (null)
    Main pointers -> the instances
    But it should be
    Main pointers & MyBidding pointers -> null
    Main pointers -> the instances
    MyBidding pointer -> Main pointers (the instances)
     
    • Winner Winner x 1
  4. I didn't even think to try reordering those functions. I really appreciate the help, thank you both!
     
    • Friendly Friendly x 1
  5. Yey, first time someone rated me winner. Thank you!
     
  6. One always wins when they do a dum-dum check! :p
     
Thread Status:
Not open for further replies.