NullPointerException when retrieving Custom Config file

Discussion in 'Plugin Development' started by NightTerror6, Jun 10, 2019.

  1. Offline

    NightTerror6

    I'm trying to make a custom configuration file, but I've been having a few issues. First of all, I get a different NullPointerException unless I make a file of the name of the configuration in the root directory of the plugin. Secondly and most prominently, when I go to get something from the file, I am given a NullPointerException.

    in onEnable():
    Code:
    characters = getCustomConfig(characters, "characters.yml");
           
    chars = new HashMap<String, String>();
    for(String x : characters.getKeys(true)) {
        String p = characters.getString(x);
        chars.put(x, p);
    }
    Config methods:
    Code:
    public void reloadCustomConfig(FileConfiguration config, String name) {
            File configFile = new File(this.getDataFolder(), name);
            if(!configFile.exists()) {
                this.saveResource(name, false);
            }
            config = YamlConfiguration.loadConfiguration(configFile);
           
            Reader defConfigStream = new InputStreamReader(this.getResource(name));
            if(defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                config.setDefaults(defConfig);
            }
        }
       
        public FileConfiguration getCustomConfig(FileConfiguration config, String name) {
            if(config == null) {
                reloadCustomConfig(config, name);
            }
            return config;
        }
       
        public void saveCustomConfig(FileConfiguration config) {
            if(config == null) {
                return;
            }
            try {
                getCustomConfig(config, config.getName()).save(new File(this.getDataFolder(), config.getName()));
            }catch(IOException e) {
                getLogger().log(Level.SEVERE, "Could not save config to " + config.getName(), e);
            }
        }
     
  2. Offline

    Kars

    The File instantiation, AKA
    Code:
    File configFile = new File(this.getDataFolder(), name);
    , loads a file where the left parameter (parent) is the directory and the right parameter (child) is the file.

    You can have it create a new file when it doesn't exist like so:
    https://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.lang.String, java.lang.String)

    That should answer your first question.

    As for the other one, 'config' is clearly null. reloadCustomConfig is broken or you are passing faulty parameters to it. I have no knowledge of YamlConfiguration but try debugging that.
     

Share This Page