Help with custom config files.

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

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


    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:
    public final class Main extends JavaPlugin{
            ConfigAccessor JobConfig;
            public void onEnable(){
            JobConfig = new ConfigAccessor(this, "Jobs.yml");
                // TODO Insert logic to be performed when the plugin is enabled
            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){
            return true;
        else {
            return false;
    Config Accessor class:
    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);
        public FileConfiguration getConfig() {
            if (fileConfiguration == null) {
            return fileConfiguration;
        public void saveConfig() {
            if (fileConfiguration == null || configFile == null) {
            } else {
                try {
                } 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);

    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(
        at org.bukkit.command.SimpleCommandMap.dispatch(
        at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(
        at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchServerCommand(
        at net.minecraft.server.v1_4_6.DedicatedServer.r(
        at net.minecraft.server.v1_4_6.MinecraftServer.q(
    Caused by: java.lang.NullPointerException
        at com.critwhale.Darkrptest.ConfigAccessor.saveDefaultConfig(
        at com.critwhale.Darkrptest.Main.onCommand(
        at org.bukkit.command.PluginCommand.execute(
        ... 8 more
    EDIT: Derp forgot to add ConfigAccessor Class
  2. Offline


    configFile is null
  3. Offline


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


    You need to define it.
  5. Offline


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


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

Share This Page