Config not being created: File cannot be null Error

Discussion in 'Plugin Development' started by Cryices, Nov 19, 2013.

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

    Cryices

    I've tried alot -_-

    this is my onEnable:

    Code:
        private FileConfiguration config;
        public final Logger logger = Logger.getLogger("Minecraft");
     
        public void onEnable() {
            File configFile = new File(this.getDataFolder() + "/config.yml");
            if(!configFile.exists())
            {
              this.saveDefaultConfig();
            }
            PluginDescriptionFile pdfFile = getDescription();
            this.logger.info(pdfFile.getName() + " V" + pdfFile.getVersion() + " Has Been Enabled!!!");
            this.config.options().copyDefaults(true);
            saveConfig();
            this.reloadConfig();
            this.saveConfig();
       
            getServer().getPluginManager().registerEvents(this, this);
        }
    any help?
     
  2. Offline

    FatAussieFatBoy

    Cryices
    You will need to change the :
    Code:java
    1. if(!configFile.exists())
    2. {
    3. this.saveDefaultConfig();
    4. }

    to :
    Code:java
    1. if(!configFile.exists())
    2. {
    3. this.getConfig().createSection("path.to.map");
    4. }
    5. // Not a Tested Code, just going off the top of my Head here


    EDIT: Also why do you have 2 this.saveConfig()'s you only need one, also you won't need a reloadConfig() in the onEnable
    Everything else how ever should be fine.
     
  3. Offline

    Cryices

    Still doesn't work, In console it says File cannot be null
     
  4. Offline

    FatAussieFatBoy

    Cryices
    You Could always try :
    Code:java
    1. if(!this.getConfig().exists()) {
    2. this.getConfig().createSection("path.to.map");
    3. }else{
    4. this.getConfig().options().copyDefaults();
    5. this.saveConfig();
    6. }
    7. //REST OF onEnable HERE


    Note: Not Tested!

    Cryices
    Here is a working Code :
    Code:java
    1. File configFile = new File(this.getDataFolder() + "/config.yml");
    2. if(configFile.exists()) {
    3. this.getConfig().options().copyDefaults();
    4. this.saveConfig();
    5. }else{
    6. this.getConfig().createSection("path.to.map");
    7. this.saveConfig();
    8. }

    Note: This has been Tested!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  5. Offline

    Cryices


    I tested by making a config, and THAT didn't work! the error is: File cannot be null
    If you know what that means this is my onEnable() sorry its messy now, i tried so much ;P
    Code:
        public void onEnable() {
            FileConfiguration config = this.getConfig();
            Logger logger = Logger.getLogger("Minecraft");
           
            config = getConfig();
            File configFile = new File(this.getDataFolder() + "/config.yml");
            if(configFile.exists()) {
            config.options().copyDefaults(true);
            this.saveConfig();
            }else{
            config.createSection("path.to.map");
            this.saveConfig();
            }
           
            PluginDescriptionFile pdfFile = getDescription();
            logger.info(pdfFile.getName() + " V" + pdfFile.getVersion() + " Has Been Enabled!!!");
           
            getServer().getPluginManager().registerEvents(this, this);
        }
    Note: I am getting int's from the config, if that has anything to do with it..
     
  6. Offline

    FatAussieFatBoy

    Cryices
    The Code I had put above should work as It is checking if a Config exists, and If it doesn't then it will create one for you.
    Steps to make sure it Works :
    Step 1 : Please check you Eclipse Directory and tell me if you have a config.yml ready. If so please paste it so I can tell what might be wrong with it.
    Step 2 : If you don't have a config.yml in the Eclipse Directory then try adding one. Also make sure that you are using 1.6.4 for your Plugin as the Code above is only tested on that Version.
     
  7. Offline

    Cryices

    bump :{
     
  8. Offline

    Skye

    What is all this? If you're working with one config file, just replace all the code concerning it in your onEnable with:
    Code:java
    1. saveDefaultConfig();

    and be sure that you've packaged a default "config.yml" in the plugin jar's base directory. saveDefaultConfig already checks if a configuration file exists before trying to copy one.

    Other things to note: Bukkit already reports the plugin being enabled/disabled, so you can delete that redundant code; and hard-coding the file separater is going to cause problems running your plugin across different OS.
     
    1Rogue likes this.
  9. Offline

    Cryices


    Code:
        public void onEnable() {
            saveDefaultConfig();
            reloadConfig();
         
            Logger logger = Logger.getLogger("Minecraft");
         
         
            PluginDescriptionFile pdfFile = getDescription();
            logger.info(pdfFile.getName() + " V" + pdfFile.getVersion() + " Has Been Enabled!!!");
         
            getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
        }
    #MyNewOnEnable
    Still doesn't work!D:< Thanks for trying!
     
  10. Offline

    rbrick

    i was getting errors with my config for my plugin. i had to create a folder with the same name that i have in my plugin.yml and then it generated the config. that may work, then again im using linux :p
     
  11. Offline

    Skye

    Did you package a config.yml with your jar?

    reloadConfig() will do nothing, so you can remove that. You only need one instance of saveDefaultConfig(), so remove the duplicate at the end. No need to copyDefaults() either.

    getLogger() will return the plugin's logger, so creating a new logger variable for use doesn't make any sense. But like I said, Bukkit already logs when plugins are enabled/disabled, so you should remove that code.
     
    1Rogue likes this.
  12. Offline

    1Rogue

    Your onEnable only needs this:

    Code:java
    1. public void onEnable() {
    2. this.saveDefaultConfig();
    3. this.getServer().getPluginManager().registerEvents(this, this);
    4. }


    And you should have a default config.yml in your resources, in the same place that you have the plugin.yml
     
    Skye likes this.
  13. Offline

    Cryices

    I do, and yet I STILL have the problem, I honestly hate things like this, that should work, but they don't for probably the most stupid of reasons
     
  14. Cryices
    In your original code, change

    Code:java
    1. File configFile = new File(this.getDataFolder() + "/config.yml");


    to

    Code:java
    1. File configFile = new File(this.getDataFolder() + "config.yml");


    You aren't properly generating the file :p
     
  15. Offline

    Skye

    If you get the same error, then you simply haven't included the config.yml resource in your plugin's jar.
     
  16. Offline

    1Rogue


    Surely you mean:

    Code:java
    1. File configFile = new File(this.getDataFolder(), "config.yml");
    2.  
    3. //or
    4.  
    5. File configFile = new File(this.getDataFolder() + File.separatorChar + "config.yml");


    Which, altogether is pointless anyhow. You don't need to check if it exists, you can just use saveDefaultConfig()
     
  17. Offline

    Cryices

    I DEFINATELY have the config.yml IN MY PLUGINS JAR! And I just use saveDefaultConfig() now. THERE SHOULDN'T BE ANY PROBLEMS!! Just give suggestions please!!! Any possible things that could be wrong. This is really annoying as i cannot find any solution that would work.
    Code:
        public void onEnable() {
            saveDefaultConfig();
            getServer().getPluginManager().registerEvents(this, this);
        }
    Isn't it a beauty? Well I have no idea why it won't work :(
    The error is File cannot be null
    And it started when i started getting ints. Am I getting them right?
    Like?
    cooldown: 15
     
  18. Lol I put a plus sign instead of a comma. Silly me
    *slaps wrist*

    It's been a long weekend :p
     
Thread Status:
Not open for further replies.

Share This Page