What is wrong with this?

Discussion in 'Plugin Development' started by dan14941, Apr 8, 2014.

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

    dan14941

    When i run the code below it gives me an error on line 49 as a NULL Unhandled exceptione xecuting this command?

    Code:
    49: if(args[1].equalsIgnoreCase("edit"))
    That is just a portion of my code.
     
  2. Offline

    RawCode

    line 49 of your copypaste is empty, provide valid code.
     
  3. Offline

    dan14941

  4. Offline

    Maurdekye

    dan14941 it's possible for args to have less items than you referenced in it, say just one (or none), and by trying to grab the second element you cause a null error.
     
  5. Offline

    dan14941

    Maurdekye this is my full code:
    Code:
    if(args[0].equalsIgnoreCase("config"))
                    {
                        player.sendMessage(ChatColor.AQUA + "+== Companies ==+");
                        player.sendMessage(ChatColor.YELLOW + "/company config [edit|add] " + ChatColor.GOLD + "Command for edditing the config");
                        if(args[1].equalsIgnoreCase("edit"))
                        {
                            player.sendMessage(ChatColor.AQUA + "+== Companies ==+");
                            player.sendMessage(ChatColor.YELLOW + "/company config edit <Config Path Name> <Config Path>");
                           
                            StringBuilder str = new StringBuilder();
                            for (int i = 3; i < args.length; i++)
                            {
                                str.append(args[i] + " ");
                            }
                            String Cpath = str.toString();
                            plugin.getConfig().set(args[2], Cpath);
                            player.sendMessage(prefix + ChatColor.GOLD + "You changed: " + args[2].toString() + " to: " + Cpath);
                            plugin.saveConfig();
                            if(args.length == 2)
                            {
                            player.sendMessage(prefix + ChatColor.RED + "Please add a config path! " + ChatColor.YELLOW + "/company config edit <Config Path Name> <Config Path>");
                            }
                        }
                    }
     
  6. Offline

    Maurdekye

    dan14941 No, it's not. Show your entire onCommand() method.
     
  7. Offline

    dan14941

    Maurdekye my full class
    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class CompanyCommand implements CommandExecutor
    {
        private final Companies plugin;
       
        public CompanyCommand(Companies passedPlugin)
        {
            this.plugin = passedPlugin;
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
           
            String prefix = ChatColor.translateAlternateColorCodes('&', "&e[Companies] ");
            Player player = (Player)sender;
           
            if(args.length > 0)
            {
                if(args[0].equalsIgnoreCase("help"))
                {
                    // creates help in chat
                    player.sendMessage(ChatColor.AQUA + "+== Companies ==+");
                    player.sendMessage(ChatColor.YELLOW + "/company list" + ChatColor.GOLD + " Shows companys");
                    player.sendMessage(ChatColor.YELLOW + "/company <company name>" + ChatColor.GOLD + " Gets information on a company.");
                    if(player.hasPermission("companies.create"))
                    {
                        player.sendMessage(ChatColor.YELLOW + "/company create <company name>" + ChatColor.GOLD + " Creates a company");
                    }
                    else if(player.hasPermission("companies.editconfig"))
                    {
                        player.sendMessage(ChatColor.YELLOW + "/company config [edit|add] " + ChatColor.GOLD + "Command for edditing the config");
                    }
                    player.sendMessage(ChatColor.YELLOW + "/company leave" + ChatColor.GOLD + " Leave your company");
                }
                else if(player.hasPermission("companies.editconfig"))
                {
                    if(args[0].equalsIgnoreCase("config"))
                    {
                        player.sendMessage(ChatColor.AQUA + "+== Companies ==+");
                        player.sendMessage(ChatColor.YELLOW + "/company config [edit|add] " + ChatColor.GOLD + "Command for edditing the config");
                        if(args[1].equalsIgnoreCase("edit"))
                        {
                            player.sendMessage(ChatColor.AQUA + "+== Companies ==+");
                            player.sendMessage(ChatColor.YELLOW + "/company config edit <Config Path Name> <Config Path>");
                           
                            StringBuilder str = new StringBuilder();
                            for (int i = 3; i < args.length; i++)
                            {
                                str.append(args[i] + " ");
                            }
                            String Cpath = str.toString();
                            plugin.getConfig().set(args[2], Cpath);
                            player.sendMessage(prefix + ChatColor.GOLD + "You changed: " + args[2].toString() + " to: " + Cpath);
                            plugin.saveConfig();
                            if(args.length == 2)
                            {
                            player.sendMessage(prefix + ChatColor.RED + "Please add a config path! " + ChatColor.YELLOW + "/company config edit <Config Path Name> <Config Path>");
                            }
                        }
                    }
                }
                else
                {
                    player.sendMessage(prefix + ChatColor.RED + "You do not have enough permission for this command!");
                }
            }
            else
            {
                player.sendMessage(prefix + ChatColor.RED + "Incorrect Syntax do /company help for commands!");
            }   
            return true;
           
        }
    }
     
  8. Offline

    Maurdekye

    dan14941 What does args contain, exactly? In your opinion.
     
  9. Offline

    dan14941

    Maurdekye if you mean by what is it meant to do its meant to if you type /company config edit it would let you edit the default config for the plugin.
     
  10. Offline

    Maurdekye

    dan14941 No, I want you to tell me what you think is inside the variable args when that method is called.
     
  11. Offline

    dan14941

  12. Offline

    Maurdekye

  13. Offline

    dan14941

  14. Offline

    Maurdekye

    dan14941 Wait, nevermind. You're trying to take the second argument out of a list of arguments, but it's possible for there only to be one argument. You have to check for that.
     
  15. Offline

    dan14941

    Maurdekye kk so my code should look like this
    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class CompanyCommand implements CommandExecutor
    {
        private final Companies plugin;
       
        public CompanyCommand(Companies passedPlugin)
        {
            this.plugin = passedPlugin;
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
           
            String prefix = ChatColor.translateAlternateColorCodes('&', "&e[Companies] ");
            Player player = (Player)sender;
           
            if(args.length > 0)
            {
                if(args[0].equalsIgnoreCase("help"))
                {
                    // creates help in chat
                    player.sendMessage(ChatColor.AQUA + "+== Companies ==+");
                    player.sendMessage(ChatColor.YELLOW + "/company list" + ChatColor.GOLD + " Shows companys");
                    player.sendMessage(ChatColor.YELLOW + "/company <company name>" + ChatColor.GOLD + " Gets information on a company.");
                    if(player.hasPermission("companies.create"))
                    {
                        player.sendMessage(ChatColor.YELLOW + "/company create <company name>" + ChatColor.GOLD + " Creates a company");
                    }
                    else if(player.hasPermission("companies.editconfig"))
                    {
                        player.sendMessage(ChatColor.YELLOW + "/company config [edit|add] " + ChatColor.GOLD + "Command for edditing the config");
                    }
                    player.sendMessage(ChatColor.YELLOW + "/company leave" + ChatColor.GOLD + " Leave your company");
                }
                else if(player.hasPermission("companies.editconfig"))
                {
                    if(args[0].equalsIgnoreCase("config"))
                    {
                        player.sendMessage(ChatColor.AQUA + "+== Companies ==+");
                        player.sendMessage(ChatColor.YELLOW + "/company config [edit|add] " + ChatColor.GOLD + "Command for edditing the config");
                        if(args[1].equalsIgnoreCase("edit"))
                        {
                            player.sendMessage(ChatColor.AQUA + "+== Companies ==+");
                            player.sendMessage(ChatColor.YELLOW + "/company config edit <Config Path Name> <Config Path>");
                            if(args.length < 3)
                            {
                                player.sendMessage(prefix + ChatColor.RED + "Please add a config path! " + ChatColor.YELLOW + "/company config edit <Config Path Name> <Config Path>");
                            }
                            else
                            {
                                StringBuilder str = new StringBuilder();
                                for (int i = 3; i < args.length; i++)
                                {
                                str.append(args[i] + " ");
                                }
                                String Cpath = str.toString();
                                plugin.getConfig().set(args[2], Cpath);
                                player.sendMessage(prefix + ChatColor.GOLD + "You changed: " + args[2].toString() + " to: " + Cpath);
                                plugin.saveConfig();
                            }
                        }
                    }
                }
                else
                {
                    player.sendMessage(prefix + ChatColor.RED + "You do not have enough permission for this command!");
                }
            }
            else
            {
                player.sendMessage(prefix + ChatColor.RED + "Incorrect Syntax do /company help for commands!");
            }   
            return true;
           
        }
    }
     
  16. Offline

    Maurdekye

    dan14941 Did you test it, does it work?
     
  17. Offline

    dan14941

    Maurdekye can't get on a computer I just guessed
     
Thread Status:
Not open for further replies.

Share This Page