Solved Internal Error everytime there are 0 arguments, or player is not online.

Discussion in 'Plugin Development' started by mrdude123, Jan 18, 2016.

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

    mrdude123

    I get an internal error everytime I enter no arguments with the command, or args[0] is an offline player.
    However, when args[0] is a valid player, it works fine.
    Code:
    package me.thecerealkill3r.banwave;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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 BanwaveAddPlayer implements CommandExecutor{
    
      
        public boolean onCommand(CommandSender sender, Command cmd, String lbl,
                String[] args) {
            if(sender instanceof Player){
            Player target = Bukkit.getPlayerExact(args[0]);
            ArrayList<Player> banlist = new ArrayList<Player>();
          
            if(args.length == 0){
                sender.sendMessage(ChatColor.RED + "No player was specified.");
                return true;
            }
            if(!target.isOnline()){
                sender.sendMessage(ChatColor.RED + "Player could not be found.");
                return true;
            }else{
                banlist.add(target);
                sender.sendMessage(ChatColor.GOLD + target.getDisplayName() + ChatColor.GREEN + " was added to the banwave.  The next banwave will take place in:");
        }
    
    
    }
            return false;
    }
    }
     
  2. Offline

    Zombie_Striker

    @mrdude123
    Could is be because you forgot to even check if there are any args? Maybe you forgot to null check? *hint,hint*
     
    0566 likes this.
  3. Offline

    adam753

    You're correctly checking if args.length is 0, but you're getting args[0] before that, making it pointless.
    The error when there's no player online is... Pretty obvious. I'm sure you'll figure it out.
     
  4. Offline

    mrdude123

    @Zombie_Striker Thank you! :)

    @adam753 After reading my stacktrace, I realized that I derped. :p
    Thanks. :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
Thread Status:
Not open for further replies.

Share This Page