Solved java.lang.ArrayIndexOutOfBoundsException: 0 Error

Discussion in 'Plugin Development' started by AGlego11, Feb 27, 2016.

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

    AGlego11

    Hey,

    So recently, I've been trying to develop a message plugin. I've got it all down, except for two things. Firstly, here is my code:
    Code:
    package me.AGlego11.list;
    
    import java.util.ArrayList;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.Sound;
    
    public class ListMain extends JavaPlugin {
    
    public Permission isStaff = new Permission ("enhancedlist.isstaff");
    public Permission canViewAdmin = new Permission ("enhancedlist.canviewadmin");
       
       
    public void onEnable () {
        getLogger().info("Ready to show the world? You bet!");
        PluginManager pm = getServer().getPluginManager();
        pm.addPermission(isStaff);
        pm.addPermission(canViewAdmin);
    
    }
       
    public void onDisable () {
       
       
    }
       
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
           
           
            if(cmd.getName().equalsIgnoreCase("ma") && sender instanceof Player) {
                Player player = (Player) sender;
                Player target = Bukkit.getServer().getPlayer(args[0]);
                String appendArgs = "";
                String playerName = player.getName();
               
               
                for (int i = 0; i < args.length; i++) {
                    appendArgs += args[i] + " ";
                   
                }
               
                if (args.length == 0) {
                    player.sendMessage(ChatColor.RED + "Incorrect format! Please use /ma (player) (message).");
                }
                else if (args.length == 1) {
                    player.sendMessage(ChatColor.RED + "Incorrect format! Please use /ma (player) (message).");
                }
               
                else {
                    player.sendMessage(playerName + ": " + appendArgs);
                    target.sendMessage(playerName + ": " + appendArgs);
                   
                }
            }
            
    (Don't worry about the brackets, there's more code, but that's for other commands and the like.)

    Anyway, whenever I run the command with 0 arguments, (just /ma), I get the java.lang.ArrayIndexOutOfBoundsException: 0 error, even though there are no items in the array at the time. Another question I have is how one would go about adding all the arguments to a list, EXCEPT for arg[0]. To clarify, if I were to run this command, /ma user hey there, I'd only want hey and there to be added to the array, not the user.

    Thanks for the help in advance.
     
  2. Offline

    mine-care

    Simple.
    args array contains 0 elements, and you are trying to get the first element, so because it has no elements in, it is throwing the exception. You need to check if the array contains the ammount of elements you want to get from it becore getting them.
     
  3. Offline

    AGlego11

    Hey,

    Thanks for your help! I can't believe I didn't notice that.
     
Thread Status:
Not open for further replies.

Share This Page