Solved 2 Reloads to load config? Nullpointer Exception

Discussion in 'Plugin Development' started by danablend, Mar 6, 2016.

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

    danablend

    So im making a plugin with custom configs. I have a config called "messages.yml" loading messages it will say if you for example dont have permission, for more customization.
    Now the problem is that, if I delete the .yml files, and reload, it will give me some errors, BUT if I reload again after the files have been created, it loads up just fine. This is the method where im loading from config, and where it is giving me errors.:
    Code:
    public void noPerm(Player player) {
            String noPermMsg = messageConfig.getString("no-permission-message");
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', noPermMsg));
        }
    The translate alternate color codes is for color support so people can do &4 etc.. for colors in messages.

    EDIT: This is my main class code:
    Code:
    package me.danablend;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import me.danablend.commands.delHomeCommand;
    import me.danablend.commands.feedCommand;
    import me.danablend.commands.healCommand;
    import me.danablend.commands.homeCommand;
    import me.danablend.commands.invseeCommand;
    import me.danablend.commands.muteCommand;
    import me.danablend.commands.setHomeCommand;
    import me.danablend.commands.unmuteCommand;
    import me.danablend.listeners.ChatListener;
    
    public class AlternateEssentials extends JavaPlugin {
    
        FileConfiguration config;
        FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "players.yml"));
        FileConfiguration messageConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "messages.yml"));
        File configFile;
        File playerFile;
        File messageFile;
        PluginManager pm;
        Logger logger = this.getLogger();
    
        @Override
        public void onEnable() {
            pm = this.getServer().getPluginManager();
            registerConfigs();
            registerCommands();
            registerPermissions();
            registerListeners();
            logger.info("Plugin successfully enabled!");
        }
    
        @Override
        public void onDisable() {
    
        }
    
        public void registerCommands() {
            logger.info("Setting up commands...");
            getCommand("home").setExecutor(new homeCommand(this));
            getCommand("sethome").setExecutor(new setHomeCommand(this));
            getCommand("delhome").setExecutor(new delHomeCommand(this));
            getCommand("mute").setExecutor(new muteCommand(this));
            getCommand("unmute").setExecutor(new unmuteCommand(this));
            getCommand("heal").setExecutor(new healCommand(this));
            getCommand("feed").setExecutor(new feedCommand(this));
            getCommand("invsee").setExecutor(new invseeCommand(this));
        }
    
        public void registerConfigs() {
            logger.info("Setting up configs...");
            this.configFile = new File(this.getDataFolder(), "config.yml");
            this.playerFile = new File(this.getDataFolder(), "players.yml");
            this.messageFile = new File(this.getDataFolder(), "messages.yml");
            config = this.getConfig();
            playerConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "players.yml"));
            messageConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "messages.yml"));
            this.saveDefaultConfig();
            this.saveDefaultPlayers();
            this.saveDefaultMessages();
        }
    
        public void registerPermissions() {
            logger.info("Setting up permissions...");
            Permission home = new Permission("altess.home");
            pm.addPermission(home);
            Permission sethome = new Permission("altess.sethome");
            pm.addPermission(sethome);
            Permission delhome = new Permission("altess.delhome");
            pm.addPermission(delhome);
            Permission mute = new Permission("altess.mute");
            pm.addPermission(mute);
            Permission unmute = new Permission("altess.unmute");
            pm.addPermission(unmute);
            Permission heal = new Permission("altess.heal");
            pm.addPermission(heal);
            Permission feed = new Permission("altess.feed");
            pm.addPermission(feed);
        }
    
        public void registerListeners() {
            logger.info("Setting up listeners...");
            new ChatListener(this);
        }
    
        public void saveConfigs() {
            this.saveConfig();
            try {
                playerConfig.save(playerFile);
                messageConfig.save(messageFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void noPerm(Player player) {
            String noPermMsg = messageConfig.getString("no-permission-message");
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', noPermMsg));
        }
    
        public void noPlayer(CommandSender sender) {
            String noPlayerMsg = messageConfig.getString("not-a-player-message");
            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', noPlayerMsg));
        }
    
        public FileConfiguration getPlayerConfig() {
            return playerConfig;
        }
    
        public FileConfiguration getMessageConfig() {
            return messageConfig;
        }
    
        public void saveDefaultMessages() {
            if (!messageFile.exists()) {
                logger.info("Setting up messages...");
                this.saveResource("messages.yml", false);
            }
        }
    
        public void saveDefaultPlayers() {
            if (!playerFile.exists()) {
                logger.info("Setting up playerdata...");
                this.saveResource("players.yml", false);
            }
        }
    
        public Logger getPluginLogger() {
            return logger;
        }
    }
     
    Last edited: Mar 6, 2016
  2. Offline

    HoeMC

    Well, tell us the errors.
     
  3. Offline

    danablend

  4. Offline

    HoeMC

    If you've deleted the file and the plugin doesn't create it after reloading and set no-permission-message then you're going to get a NPE. Do you call saveDefaultConfig() before trying to retrieve values? If so, does your default yml contain a value for no-permission-message?
     
  5. Offline

    Ridan

    Seems like noPermMsg is null, meaning the field no-permission-message contains nothing.
     
  6. Offline

    danablend

    My messages.yml gets created before being used. The data isnt used before a player without permissions types a command. You can view all the code for the configs above in the main post.

    I fixed the problem on my own. In the methods where I used the configs to get string, I added my "registerConfigs()" method, so it registers them before the getting of string. For people who will have this problem in the future, this is my registerConfigs method for my custom configs:
    Code:
        public void registerConfigs() {
            logger.info("Setting up configs...");
            this.messageFile = new File(this.getDataFolder(), "messages.yml");
            this.configFile = new File(this.getDataFolder(), "config.yml");
            this.playerFile = new File(this.getDataFolder(), "players.yml");
            messageConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "messages.yml"));
            config = this.getConfig();
            playerConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "players.yml"));
            this.saveDefaultMessages();
            this.saveDefaultConfig();
            this.saveDefaultPlayers();
        }


    I fixed the problem on my own. In the methods where I used the configs to get string, I added my "registerConfigs()" method, so it registers them before the getting of string. For people who will have this problem in the future, this is my registerConfigs method for my custom configs:
    Code:
        public void registerConfigs() {
            logger.info("Setting up configs...");
            this.messageFile = new File(this.getDataFolder(), "messages.yml");
            this.configFile = new File(this.getDataFolder(), "config.yml");
            this.playerFile = new File(this.getDataFolder(), "players.yml");
            messageConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "messages.yml"));
            config = this.getConfig();
            playerConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "players.yml"));
            this.saveDefaultMessages();
            this.saveDefaultConfig();
            this.saveDefaultPlayers();
        }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 6, 2016
Thread Status:
Not open for further replies.

Share This Page