Solved Null pointer error when loading custom config

Discussion in 'Spigot Plugin Development' started by Flyin, Jan 9, 2016.

  1. Hey Spigot! I am having a small problem when loading/creating a custom config. I have made a class to handle making custom configs and I know that that works fine. If I load/create the config in the onEnable() it works fine but if I try to do it in another class it throws this error. Any help in fixing this error would be much appreciated.

    Error:
    Code (Text):
    [21:18:54 ERROR]: Error occurred while enabling DynamicHub v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at me.flyin.dynamichub.DynamicHub.onEnable(DynamicHub.java:20) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [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 org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [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(Thread.java:745) [?:1.8.0_66]
     
    Main Class
    Code (Text):
    public class DynamicHub extends JavaPlugin implements Listener {

        PluginManager pm = Bukkit.getPluginManager();

        private Config conf;

        public void onEnable()
        {
            conf.loadConfig(this);
        }

        public void onDisable()
        {

        }
    Config Class
    Code (Text):
    public class Config {

        private DynamicFileConfiguration conf;

        public void loadConfig(DynamicHub plugin)
        {
            //this makes the config. I call this in the onEnable()
            this.conf = new DynamicFileConfiguration(plugin, "config.yml", "config.yml");
        }

        public void reloadConfig()
        {
            //reloads config
            conf.reload();
        }

        public void saveConfig()
        {
            //saves config
            conf.save();
        }

        public FileConfiguration getConfig()
        {
            //gets config
            return conf;
        }
    }
    DynamicFileConfiguration Class
    Code (Text):
    public class DynamicFileConfiguration extends YamlConfiguration
    {

        public DynamicFileConfiguration(DynamicHub i)
        {
            this.plugin = i;
        }

        private File file;
        private String defaults;
        private DynamicHub plugin;

        public DynamicFileConfiguration(DynamicHub plugin, String fileName, String defaultsFile)
        {
            this.plugin = plugin;
            this.defaults = defaultsFile;
            this.file = new File(plugin.getDataFolder(), fileName);
            reload();
        }

        public void save()
        {
            try {
                options().indent(2);
                save(file);
            } catch (IOException exception) {
                plugin.getLogger().severe("[DynamicHub] Error, could not save '" + file.getName() + "'.");
            }
        }

        public void reload()
        {
            if (!file.exists())
            {
                try {
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                } catch (IOException exception) {
                    exception.printStackTrace();
                    plugin.getLogger().severe("[DynamicHub] Error, failed to create '" + file.getName() + "'.");
                }
            }
            try {
                load(file);

                if (!(defaults == null)) {
                    InputStreamReader reader = new InputStreamReader(plugin.getResource(defaults));
                    FileConfiguration defaultsConfig = YamlConfiguration.loadConfiguration(reader);

                    setDefaults(defaultsConfig);
                    options().copyDefaults(true);

                    reader.close();
                    save();
                }

            } catch (IOException exception) {
                exception.printStackTrace();
                plugin.getLogger().severe("Error while loading file " + file.getName());

            } catch (InvalidConfigurationException exception) {
                exception.printStackTrace();
                plugin.getLogger().severe("Error while loading file " + file.getName());
            }
        }
    }
     
  2. Your "conf" field in your main class has not been initialised.
     
    • Agree Agree x 1
  3. EDIT: Thanks so much m8! That was it. +1
     
    #3 Flyin, Jan 9, 2016
    Last edited: Jan 9, 2016