Check if Null, but still internal error plus stacktrace.

Discussion in 'Plugin Development' started by S1ant, May 14, 2017.

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

    S1ant

    So, I am coding a plugin and I check if the player is null but when I run the command on a person online it still says they're null. So, I gave it a message to send if they see that the player they run the command on is null, but it still has an internal error!

    Code:
    if(args[0] != null){
                
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                    
                        sender.sendMessage(Blocking out for secrecy :) );
                
                    }
                
                    if(args[0] == null) {
        sender.sendMessage( "That player Isn't online!");
                    }
     
  2. Offline

    DuaneTheDev_

    Don't check if the argument is null, that just means you didn't type anything after the command. Check if the target is null. @S1ant

    Example:
    Code:
    if(commandLabel.equalsIgnoreCase("check")) {
                //Check if the command arguments are 0, meaning if the player only typed "check"
                if(args.length == 0) {
                    //Then tell them a correct usage
                    p.sendMessage("/check (name)");
                    //Else if the player entered the correct amount of arguments which is 1
                } else if(args.length == 1) { //One stands for the first argument (name) in /check (name)
                    //Define the target
                    Player target = Bukkit.getServer().getPlayer(args[0]); //Please note; this isn't an efficent way of getting a target player
                    //Check if the target is null
                    if(target == null) {
                        //If null send message
                        p.sendMessage(ChatColor.RED + "Sorry! I can't find " + args[0] + "! I'll grab my glasses next time.");
                        //Message sent, so it worked, return true
                        return true;
                      
                        //Else if NOT null meaning Online
                    } else {
                        //Send message
                            p.sendMessage(ChatColor.GREEN + "I found the player!");
                    }
                }
            }
     
  3. Offline

    S1ant

    @DuaneTheDev_ Ok, I looked over the code, but apparently didn't use my common java sense. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page