Solved set defaults custom config file error

Discussion in 'Plugin Development' started by GijsCo, Feb 21, 2018.

Thread Status:
Not open for further replies.
  1. I have this problem with custom config.yml files. I just don't seem to get it working for some reason.

    I got an null pointer exception on the line:
    super.options.copyDefaults(true);

    I also set an commend where the error is finding place.

    This cofig file always worked perfectly fine, I just don't get it why it's giving me an error

    Code:
    package hub.utiles;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    import hub.Core;
    
    public class Config extends YamlConfiguration
    {
       private Core core;
       
       private File file;   
       private String saveName;
       private HashMap<String, Object> defaults;
       
       public Config(Core core, String saveName, HashMap<String, Object> defaults)
       {
         this.core = core;
         this.saveName = saveName.endsWith(".yml") ? saveName : saveName + ".yml";
         this.defaults = defaults;
         
         if(saveName.equals("") || saveName.equals(null))
            throw new IllegalArgumentException("Save name cannot be null!");
         
         file = new File(core.getDataFolder().getAbsolutePath() + File.separatorChar + saveName);
         file.getParentFile().mkdirs();
    
         try
         {
           if(!file.exists())
           {
             if(!file.canWrite() || file.canRead())
             {
               file.setWritable(true, false);
               file.setReadable(true, false);
             }
             
             file.createNewFile();
           }
           if(defaults != null)
           {
             super.addDefaults(defaults);
             super.options.copyDefaults(true); // this lines gives an error
             save();
           }
           load(file);
         }
         catch (IOException | InvalidConfigurationException e)
         {
           e.printStackTrace();
         }
       }
       
       public void save()
       {
         try
         {
           save(file);
         }
         catch (IOException e)
         {
           e.printStackTrace();
         }
       }
    }
    
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @GijsCo What error do you get?
     
  3. @timtower
    Sorry forgot to add that, I get an null pointer exception.
    To mention is that I also tried: super.addDefault("test", "test1");

    But than still it's giving me an null pointer exception :/
     
  4. Offline

    timtower Administrator Administrator Moderator

    @GijsCo You never call the super constructor, the entire YamlConfiguration is probably null.
     
  5. @timtower
    Oh I must have overlooked that, dump me ^^.

    Thanks for helping!
     
    timtower likes this.
Thread Status:
Not open for further replies.

Share This Page