Help with custom .yml files

Discussion in 'Plugin Development' started by alex123099, Jul 23, 2013.

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

    alex123099

    I'm trying to make use of my own custom .yml config files, however, whenever I start the server with my plugin, I get the following error message:
    Code:
    23:04:03 [SEVERE] Error occurred while enabling VotePoints v3.1.0 (Is it up to d
    ate?)
    java.lang.NullPointerException
            at handlers.ConfigHandler.saveDefaultConfig(ConfigHandler.java:65)
            at core.VPCore.initHandlers(VPCore.java:39)
            at core.VPCore.onEnable(VPCore.java:16)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:457)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:381)
            at org.bukkit.craftbukkit.v1_5_R3.CraftServer.loadPlugin(CraftServer.jav
    a:282)
            at org.bukkit.craftbukkit.v1_5_R3.CraftServer.enablePlugins(CraftServer.
    java:264)
            at net.minecraft.server.v1_5_R3.MinecraftServer.j(MinecraftServer.java:3
    04)
            at net.minecraft.server.v1_5_R3.MinecraftServer.e(MinecraftServer.java:2
    83)
            at net.minecraft.server.v1_5_R3.MinecraftServer.a(MinecraftServer.java:2
    43)
            at net.minecraft.server.v1_5_R3.DedicatedServer.init(DedicatedServer.jav
    a:151)
            at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java
    :382)
            at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:5
    73)
    
    The null-pointer points me to:
    Code:
    vpc.saveResource(configFile.getName(), false);
    
    ConfigHandler:
    Code:
    public class ConfigHandler {
     
    public static ConfigHandler coreConfigHandler,
    playerDataConfigHandler,
    shopConfigHandler,
    shopItemConfigHandler;
     
    public static ArrayList<ConfigHandler> configHandlers = new ArrayList<ConfigHandler>(); 
     
    private VPCore vpc;
     
    private File configFile;
    private FileConfiguration config;
     
    public ConfigHandler(VPCore vpc, File configFile){
    this.configFile = configFile;
    config = YamlConfiguration.loadConfiguration(configFile);
    configHandlers.add(this);
    }
     
    public void reloadConfig() { 
    config = YamlConfiguration.loadConfiguration(configFile);
     
            InputStream defConfigStream = vpc.getResource(configFile.getName());
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                config.setDefaults(defConfig);
            }
    }
     
    public FileConfiguration getConfig() {
            if (config == null)
                reloadConfig();
            
            return config;
        }
     
    public void saveConfig() {
            if (config == null || configFile == null)
                return;
            else
                try {
                    getConfig().save(configFile);
                } catch (IOException ex) {
                    vpc.getLogger().log(Level.SEVERE, "Could not save config to " + configFile, ex);
                }
        }
     
    public void saveDefaultConfig() {
            if (!configFile.exists()) {
                vpc.saveResource(configFile.getName(), false);
            }
        }
     
    public String getFileName(){
    return configFile.getName();
    }
    }
    
    VPCore:
    Code:
    public class VPCore extends JavaPlugin{
     
    public void onEnable(){
    checkVaultDependency();
    initHandlers();
    initExtras();
    getCommand("vp").setExecutor(new CommandHandler(this));
    this.getLogger().info("VotePoints v" + this.getDescription().getVersion() + " enabled.");
    }
     
    public void onDisable(){
    this.getLogger().info("Disabling VotePoints. Goodbye!");
    }
     
    public File getPluginFile(){
    return this.getFile();
    }
     
    private void initHandlers(){
    //init configHandlers
    ConfigHandler.coreConfigHandler = new ConfigHandler(this, new File(this.getDataFolder(), "config.yml"));
    ConfigHandler.playerDataConfigHandler = new ConfigHandler(this, new File(this.getDataFolder(), "playerData.yml"));
    ConfigHandler.shopConfigHandler = new ConfigHandler(this, new File(this.getDataFolder()+"/shop","shopConfig.yml"));
    ConfigHandler.shopItemConfigHandler = new ConfigHandler(this, new File(this.getDataFolder()+"/shop","shopItemConfig.yml"));
     
    //create config files if non-existent
    for(int i=0; i<ConfigHandler.configHandlers.size(); i++)
    ConfigHandler.configHandlers.get(i).saveDefaultConfig();
     
    //init UpdateHandler
    UpdateHandler.updateHandler = new UpdateHandler(this);
     
    //init taskHandler
    TaskHandler.taskHandler = new TaskHandler(this);
    }
     
    private void initExtras(){
    //init history
    History.history = new History(this);
    }
     
    private void checkVaultDependency(){
    if(getServer().getPluginManager().getPlugin("Vault")==null)
    {
    this.getLogger().log(Level.SEVERE, "Vault plugin required. VotePoints disabled.");
    getServer().getPluginManager().disablePlugin(this);
    return;
    }
    }
    }
    
    I don't even know what's causing it, most likely some little thing I forgot as this is the first time I'm working with YAML files.
     
  2. Offline

    SnipsRevival

    I am fairly certain vpc is null as I don't see it being initialized.
     
  3. Offline

    alex123099

    It is sent to the confighandler constructor. Vpcore is the main class
     
  4. Offline

    SnipsRevival

    alex123099 You would need to add this.vpc = vpc to your ConfigHandler constructor.
     
  5. Offline

    alex123099

    OMG of course :D I knew it was something stupid like that, at first I forgot to initialize the configFile, now the vpc, guess I shouldn't be coding at midnight :D
    Thank you!

    Another question:
    When I do the saveDefaultConfig function, if the .yml file does not exist, it will take it from the jar and create it. This works perfectly for the files that are in the same location as plugin.yml, however, what if I have other .yml files which are in a folder inside the jar, how would I generate the folder with the files if it does not exist?
     
Thread Status:
Not open for further replies.

Share This Page