Development Assistance Custom YAML File Access Issue

Discussion in 'Plugin Help/Development/Requests' started by DudeGuyManPerson, Feb 5, 2015.

Thread Status:
Not open for further replies.
  1. So I'm working on a plugin in which I need a YAML file to temporarily store information about conversations I created, and I'm having trouble with NullPointerExceptions. It only occurs when I try to use two of my commands, and occurs in the for loop that goes through the keys in my config file. I think it's due to the fact that I set up my custom Yml file incorrectly, so it can't access it in my classes, but it might be something else entirely. Also, ignore my comments about the hashmaps in the code, I was originally trying to use one instead of the yml file, but recently decided to switch.

    Here's my main class where I attempted to set up the file (Note I have the regular config file, plus this additional one):
    Code (open)
    Code removed, issue changed.


    Here's part of my commands class (leaving out irrelevant commands):
    Code (open)
    Code removed, issue changed.

    Any help is greatly appreciated. Also, just ask if there's any more information I forgot to include.
     
    Last edited: Feb 11, 2015
  2. Offline

    mine-care

    When referring to temp storage it's good to help it in Armand not put a headache to the server that is trying to save and load files every so, I strongly recommend you go back to hashmaps.

    I couldn't trace where your NPE occurred but it would be great if you could provide the line and stacktrace. Thanks
     
  3. @mine-care Ok I'll go back to using hashmaps. I was having this same problem earlier - a NPE when using for loops to go through the hashmap - but I think it might have been because I was experimenting with another save file (not yaml) and it was connected, although I didn't think it would be a problem. However, I still want to figure out how to add a custom yaml file, in case I have to use it later on.

    Here's a picture of my most recent stack trace. http://puu.sh/fwY8n/ed1b498768.png
    Right now, it's pointing to what is line 34 in the commands class I posted (different line number due to me taking out other commands). The NPE makes me think that it's an issue with how I'm checking to see if the configuration section in my chatHelpConfig is empty, not simply accessing the YML file, because it doesn't point to the line before it that gets the configSection from the file (Could be completely wrong, I'm still new to this).
     
  4. Online

    timtower Administrator Administrator Moderator

    Moved to Bukkit alternatives.
    @DudeGuyManPerson Please use pastebin instead for logs.
     
  5. Offline

    mine-care

    @DudeGuyManPerson
    Check if line 33 is null, because on line 34 you invoke a method on something that might be null. :)
     
  6. Ok so I rewrote it using a variety of ArrayLists and HashMaps, and everything's working fine for that now. However, I'm having trouble with the default plugin config, so I'm still trying to make a new yaml file. I tried a different method this time, and I think I'm almost there, but I'm getting a NPE when I try to access the plugin's data folder.

    Here's the class I made to contain all the methods for managing my config file. It works fine, it's just line 29 that I keep getting errors on. The funny thing (at least to me) is that after loading up the server, the plugin still generates the data folder and the config file with the defaults I made, but I still get an NPE with the data folder.
    Code (open)

    Code:
    package com.modtools.main;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.util.logging.Level;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public class ConfigAccess {
       
    public ModToolsMain plugin;
       
        private FileConfiguration customConfig;
        private File customConfigFile;
       
        public ConfigAccess(ModToolsMain plugin) {
            this.plugin = plugin;
            /**File dataFolder = plugin.getDataFolder();
            if (dataFolder == null)
                throw new IllegalStateException();
            this.customConfigFile = new File(plugin.getDataFolder(), "config.yml");**/
        }
    
        public void reloadCustomConfig() {
            if (customConfigFile == null) {
                customConfigFile = new File(plugin.getDataFolder(), "config.yml");
            }
            customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
           
            Reader defConfigStream = new InputStreamReader(plugin.getResource("config.yml"));
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                customConfig.setDefaults(defConfig);
            }
        }
       
        public FileConfiguration getCustomConfig() {
            if (customConfig == null) {
                reloadCustomConfig();
            }
            return customConfig;
        }
       
        public void saveCustomConfig() {
            if (customConfig == null || customConfigFile == null) {
                return;
            }
            try {
                getCustomConfig().save(customConfigFile);
            } catch (IOException ex) {
                plugin.getLogger().log(Level.SEVERE, "Could not save customConfig to " + customConfigFile, ex);
            }
        }
       
        public void saveDefaultCustomConfig() {
            if (customConfigFile == null) {
                customConfigFile = new File(plugin.getDataFolder(), "config.yml");
            }
            if (!customConfigFile.exists()) {
                plugin.saveResource("config.yml", false);
            }
        }
       
    }
    


    Here's my onEnable method in my main class:
    Code (open)

    Code:
    ConfigAccess access = new ConfigAccess(this);
       
        public static Plugin plugin;
       
        public void onEnable() {
            plugin = this;
           
            File dir = plugin.getDataFolder();
            if (!dir.exists())
                if(!dir.mkdir())
                    System.out.println("Could not create directory for plugin: " + getDescription().getName());
           
            access.saveDefaultCustomConfig();
            access.saveCustomConfig();
            registerEvents(this, new HelpChat(this), new StaffJoin(this));
            getCommand("staffhelp").setExecutor(new Commands(this));
            getCommand("endhelp").setExecutor(new Commands(this));
            getCommand("joinhelp").setExecutor(new Commands(this));
            getCommand("seehelp").setExecutor(new Commands(this));
            getCommand("leavehelp").setExecutor(new Commands(this));
           
            if (!plugin.getConfig().contains("staff")) {
                plugin.getConfig().addDefault("staff", null);
            }
           
            convos.clear();
            members.clear();
            total.clear();
        }


    And here's the NPE I get in the console:
    http://pastebin.com/PQxu6Rgc
     
Thread Status:
Not open for further replies.

Share This Page