Help with custom config files.

Discussion in 'Plugin Development' started by CritWhale, Jan 22, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    CritWhale

    I'm a newer program and I've been severely struggling figuring out why this doesn't work. I'm trying to get data from a file called Jobs.Yml, and It returns Null Pointer Exception almost every time I've thought fixed the plugin. It would be greatly appreciated if someone could explain to me what I'm doing wrong, and point me in the right direction.

    Here's my Main Class:
    Code:
    public final class Main extends JavaPlugin{
            ConfigAccessor JobConfig;
         
            @Override
            public void onEnable(){
            JobConfig = new ConfigAccessor(this, "Jobs.yml");
            JobConfig.saveDefaultConfig();
         
                // TODO Insert logic to be performed when the plugin is enabled
            }
            @Override
            public void onDisable() {
                // TODO Insert logic to be performed when the plugin is disabled
            } 
         
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        if(cmd.getName().equalsIgnoreCase("Job")){
            System.out.println();
            return true;
            }
        else {
            return false;
            }
        }
    }
    Config Accessor class:
    Code:
    public class ConfigAccessor {
     
        private final String fileName;
        private final JavaPlugin plugin;
       
        private File configFile;
        private FileConfiguration fileConfiguration;
     
        public ConfigAccessor(JavaPlugin plugin, String fileName) {
            if (plugin == null)
                throw new IllegalArgumentException("plugin cannot be null");
            if (!plugin.isInitialized())
                throw new IllegalArgumentException("plugin must be initiaized");
            this.plugin = plugin;
            this.fileName = fileName;
        }
     
        public void reloadConfig() {
            if (configFile == null) {
                File dataFolder = plugin.getDataFolder();
                if (dataFolder == null)
                    throw new IllegalStateException();
                configFile = new File(dataFolder, fileName);
            }
            fileConfiguration = YamlConfiguration.loadConfiguration(configFile);
     
            // Look for defaults in the jar
            InputStream defConfigStream = plugin.getResource(fileName);
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                fileConfiguration.setDefaults(defConfig);
            }
        }
     
        public FileConfiguration getConfig() {
            if (fileConfiguration == null) {
                this.reloadConfig();
            }
            return fileConfiguration;
        }
     
        public void saveConfig() {
            if (fileConfiguration == null || configFile == null) {
                return;
            } else {
                try {
                    getConfig().save(configFile);
                } catch (IOException ex) {
                    plugin.getLogger().log(Level.SEVERE, "Could not save config to " + configFile, ex);
                }
            }
        }
       
        public void saveDefaultConfig() {
            if (!configFile.exists()) {           
                this.plugin.saveResource(fileName, false);
            }
        }
     
    }


    Code:
    Preparing start region for level 1 (Seed: 475185218736544969)
    2013-01-22 09:35:40 [INFO] Preparing start region for level 2 (Seed: 475185218736544969)
    2013-01-22 09:35:40 [INFO] [WorldEdit] Enabling WorldEdit v5.5
    2013-01-22 09:35:40 [INFO] WEPIF: Using the Bukkit Permissions API.
    2013-01-22 09:35:40 [INFO] [Darkrptest] Enabling Darkrptest v1.0
    2013-01-22 09:35:40 [INFO] Server permissions file permissions.yml is empty, ignoring it
    2013-01-22 09:35:40 [INFO] Done (1.180s)! For help, type "help" or "?"
    2013-01-22 09:35:44 [WARNING] Unexpected exception while parsing console command "job"
    org.bukkit.command.CommandException: Unhandled exception executing command 'job' in plugin Darkrptest v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
        at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer.java:514)
        at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchServerCommand(CraftServer.java:506)
        at net.minecraft.server.v1_4_6.DedicatedServer.al(DedicatedServer.java:260)
        at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:225)
        at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
        at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
        at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.NullPointerException
        at com.critwhale.Darkrptest.ConfigAccessor.saveDefaultConfig(ConfigAccessor.java:87)
        at com.critwhale.Darkrptest.Main.onCommand(Main.java:25)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 8 more
    EDIT: Derp forgot to add ConfigAccessor Class
     
  2. Offline

    fireblast709

    configFile is null
     
  3. Offline

    CritWhale

    Well when I initiate JobConfig in my onEnable shouldn't that change it from null to "Jobs.yml"?
     
  4. Offline

    Th3Controller

    You need to define it.
     
  5. Offline

    fireblast709

    CritWhale no you set the filename, but never initialize the File configFile
     
  6. Offline

    CritWhale

    So In my Main Class, I need something along the lines of...
    Code:
    ConfigAccessor ConfigFile = something;
    Sorry Like I said I'm pretty new to this.
     
Thread Status:
Not open for further replies.

Share This Page