Command Error

Discussion in 'Plugin Development' started by scorbin60943, Jan 21, 2013.

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

    scorbin60943

    I am trying to add a command to my plugin that will be able to set config variables

    I get this error
    Code:
    [INFO] Stomper Enabled | Version 0.3
    2013-01-21 16:37:16 [INFO] scorbin60943 issued server command: /config StompRange 6
    2013-01-21 16:37:16 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'config' in plugin Stomper v0.3
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
        at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer.java:514)
        at net.minecraft.server.v1_4_6.PlayerConnection.handleCommand(PlayerConnection.java:979)
        at net.minecraft.server.v1_4_6.PlayerConnection.chat(PlayerConnection.java:897)
        at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:852)
        at net.minecraft.server.v1_4_6.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
        at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:112)
        at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
        at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:598)
        at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
        at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
        at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
        at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.NullPointerException
        at me.scorbin60943.stomper.commands.CommandConfig.onCommand(CommandConfig.java:19)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
    Main Class
    Code:
    package me.scorbin60943.stomper;
     
    import java.util.logging.Logger;
     
    import me.scorbin60943.stomper.commands.CommandConfig;
    import me.scorbin60943.stomper.events.PlayerListener;
     
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Stomper extends JavaPlugin implements Listener{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Stomper plugin;
     
            @Override
            public void onEnable(){
               
                getCommand("Config").setExecutor(new CommandConfig());
                this.logger.info("- Loaded Commands");
               
                PluginManager pm = getServer().getPluginManager();
                pm.registerEvents(new PlayerListener(this), this);
                this.logger.info("- Loaded Events");
               
                loadConfig();
                this.logger.info("- Loaded Config");
               
                PluginDescriptionFile pdfFile = this.getDescription();
                this.logger.info(pdfFile.getName() + " Enabled" + " | Version " + pdfFile.getVersion());
            }
           
            public void loadConfig(){
                  getConfig().options().copyDefaults(true);
                  saveConfig();
                }
           
            @Override
            public void onDisable() {
                saveConfig();
                this.logger.info("- Saved Config");
               
                PluginDescriptionFile pdfFile = this.getDescription();
                this.logger.info(pdfFile.getName() + " Disabled");
                }
    }
    Command Class
    Code:
    package me.scorbin60943.stomper.commands;
     
    import me.scorbin60943.stomper.Stomper;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
     
    public class CommandConfig implements CommandExecutor {
        private Stomper plugin;
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("config") && args.length >= 2){
                String var = args[1];//should this be 0
                String set = args[2];
                if (args.length == 2) {
                    plugin.getConfig().set(var, set);
                    sender.sendMessage(ChatColor.YELLOW + var +" Set to " + set);
                    return true;
                }else{
                    sender.sendMessage(ChatColor.DARK_RED + "Invalid Arguments");
                    sender.sendMessage(ChatColor.DARK_RED + "config <variable> <set>");
                }
            }
            return false;
        }
    }
    
    Please help.
     
  2. Offline

    nisovin

    Your plugin variable is null. You will need to pass an instance of your plugin to your CommandConfig class in the same way you do with your listener.
     
    ZeusAllMighty11 likes this.
  3. Offline

    scorbin60943

    It didn't work I just get a new error.

    Code:
    [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'config' in plugin Stomper v0.3
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
        at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer.java:514)
        at net.minecraft.server.v1_4_6.PlayerConnection.handleCommand(PlayerConnection.java:979)
        at net.minecraft.server.v1_4_6.PlayerConnection.chat(PlayerConnection.java:897)
        at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:852)
        at net.minecraft.server.v1_4_6.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
        at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:112)
        at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
        at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:598)
        at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
        at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
        at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
        at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
        at me.scorbin60943.stomper.commands.CommandConfig.onCommand(CommandConfig.java:24)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
     
  4. Offline

    nisovin

    In the place where it says "should this be 0" the answer is yes, it should. Array indexes begin at 0.
     
  5. Offline

    scorbin60943

    I should have gone with my gut.

    I got the same error as the first time.

    in my listerner I use this
    Code:
        public PlayerListener(Stomper plugin){
            this.plugin = plugin;
            StompRange = plugin.getConfig().getInt("StompRange");
            StomperMinFall = plugin.getConfig().getInt("StomperMinFall");
            StompDamage = plugin.getConfig().getInt("StompDamage");
        }
    but In the command class I just have
    "this.plugin = plugin"
    and I don't add the variables because Im not using them Im letting the sender select them but this causes a error.

    Help Any one?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  6. Offline

    AstramG

    Make sure in your default config file you have each thing marked down such as
    StompRange:
    StomperMinFall:
    StompDamage:
    If you don't have that criteria in the file it won't work because what you're getting from the config wouldn't exist and would throw an error.
     
Thread Status:
Not open for further replies.

Share This Page