NullPointerError (This is my first plugin so please explain) config.yml

Discussion in 'Plugin Development' started by Abusinq, Oct 22, 2020.

  1. Offline


    I am writing a plugin to do the following: /mode {gamemode} {player} permission: mode.change

    Example /mode 1 (creative) (if nothing is defined for player it defaults to self)

    Example 2: /mode 0 iAbusinq (Sets my gamemode to 0, can be run by anyone with the permission.)

      noPermission: "&cYou do not have permission to use this command."
      AlreadyCreative: "&cYou are already in creative mode!"
      GamemodeCreativeSelf: "&aYou set your gamemode to &eCREATIVE&a."
      Usage: "&aType /help when I get around to coding it. :P"
    Main Class (Gamemode):
    package com.abusinq.Gamemode;
    import org.bukkit.ChatColor;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    public class Gamemode extends JavaPlugin implements Listener {
        private Commands commands = new Commands();
        public void onEnable() {
            PluginDescriptionFile PluginYML = this.getDescription();
            String ver = PluginYML.getVersion();
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\nHubGamemode has been enabled.\n" + ChatColor.GREEN + "Currently running version " + ver + ".\n");
        public void onDisable() {
            getServer().getConsoleSender().sendMessage(ChatColor.DARK_RED + "\n\nHubGamemode has been disabled.\n\n");
        public void loadConfig() {
    Commands Class:
    package com.abusinq.Gamemode;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    public class Commands implements Listener, CommandExecutor {
        private Gamemode plugin;
        public void Gamemode (Gamemode plugin) {
            this.plugin = plugin;
        public String cmd1 = "mode";
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (sender instanceof Player){
                if (cmd.getName().equalsIgnoreCase(cmd1)) {
                    if (args.length != 0) {
                    //Setting gamemode creative
                    if (player.hasPermission("gamemode.change")) {
                        if (args[0].equalsIgnoreCase("c")) {
                            if (!player.getGameMode().equals(GameMode.CREATIVE)) {
                                player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.GamemodeCreativeSelf")));
                            } else {
                                player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.AlreadyCreative")));
                    } else {
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.noPermission")));
                    //Next gamemode thing here
                    } else {
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.Usage")));
            } else {
                sender.sendMessage(ChatColor.DARK_RED + "Only players can execute this command.");
            return false;
    How do I get the plugin to read config.yml??? It just keeps sending null.
    Last edited: Oct 22, 2020
  2. Offline


    Does the config file gets created?
    If not, try changing saveConfig() to saveDefaultConfig()

    Btw, the noPerm and usage messages are redundant because they are...or should be in the config.yml
  3. Offline


    Agreed. I just put "saveDefaultConfig();" in my plugins, nothing else, and it populates config.yml from the one in my plugin if it doesn't already exist.

    Also unnecessary is checking if cmd.getName() is cmd1; that's the only command with that class set as executor. It could be nothing but that command.

    Also also, checking if sender instanceof Player after casting it thus is "putting the cart before the horse" ;)

    Last, I'd suggest using negative check and return instead of positive checks. It will remove the unnecessary indentation, stopping your methods from looking like arrows heh.

Share This Page