Solved ArrayList null?

Discussion in 'Spigot Plugin Development' started by Sean0402, Feb 2, 2020.

Thread Status:
Not open for further replies.
  1. Hello, I'm trying to add a target player to my ArrayList (Which it does fine from debugging), but when I go to grab that player again, it says their null from debugging. Any ideas? (On accept / deny if statements)

    Error lines;
    Code (Java):
                        if (BluePara.getInstance().playersAcceptDeny.remove(target.getUniqueId())) {
    Class: (It's a bit messy right now until I fix things up. This is purely to get things tested).
    Code (Java):
    ublic class Commands implements CommandExecutor {

        private double price;
        Player target;

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(Methods.color(BluePara.getInstance().getConfig().getString("NoConsole")));
            }
            if (sender instanceof Player) {
                price = BluePara.getInstance().getConfig().getDouble("Price");
                Player p = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("heal") && sender.hasPermission("blue.para.heal")) {
                    if (args.length == 0) {
                        p.sendMessage(Methods.color(BluePara.getInstance().getConfig().getString("Usage")));
                        return true;
                    }
                    target = Bukkit.getPlayer(args[0]);

                    if (target != null) {
                        //todo: Add this back. Simply for testing on myself
                        /*if (targetPlayer == sender) {
                            p.sendMessage(Methods.color(BluePara.getInstance().getConfig().getString("CannotHealSelf")));
                            return false;
                        }*/

                        if (!BluePara.getInstance().playersAcceptDeny.contains(target.getUniqueId())) {
                            p.sendMessage(Methods.color(BluePara.getInstance().getConfig().getString("SentHeal").replace("%player%", target.getName())));
                            BluePara.getInstance().playersAcceptDeny.add(target.getUniqueId());
                            Methods.hoverRunCmd(target, BluePara.getInstance().getConfig().getString("Accept").replace("%player%", p.getName()).replace("%price%", Double.toString(price)), "/heal accept", BluePara.getInstance().getConfig().getString("Accept").replace("%player%", p.getName()));
                            Methods.hoverRunCmd(target, BluePara.getInstance().getConfig().getString("Deny").replace("%player%", p.getName()), "/heal deny", BluePara.getInstance().getConfig().getString("Deny").replace("%player%", p.getName()));
                        } else {
                            p.sendMessage("Player already has a pending heal");
                        }
                    }
                    if (args[0].equalsIgnoreCase("accept")) {
                        p.sendMessage("test");
                        target.sendMessage(Methods.color(BluePara.getInstance().getConfig().getString("AcceptHeal").replace("%player%", p.getName()).replace("%price%", Double.toString(price))));
                        p.sendMessage(Methods.color(BluePara.getInstance().getConfig().getString("TargetAccepted").replace("%player%", target.getName())));
                    }
                    if (args[0].equalsIgnoreCase("deny")) {
                        if (BluePara.getInstance().playersAcceptDeny.contains(target.getUniqueId())) {
                            BluePara.getInstance().playersAcceptDeny.remove(target.getUniqueId());
                            Bukkit.broadcastMessage("removing");
                        }
                    }
                }
            }
            return false;
        }
     
  2. It says they're null because they probably are. The accept / deny if statements are outside the original null check for the target player.
     
    • Agree Agree x 1
  3. Show the code where you create you arraylist and declare it
     
  4. At the top of the main class.
    Code (Java):
        public Set<UUID> playersAcceptDeny = new HashSet<>();
     
     
  5. This goes into the onEnable() method: playersAcceptDeny = new HashSet<>();
    And at the top there's only supposedto be public Set<UUID> playersAcceptDeny;
     
  6. That shouldn't matter in this case as it's adding them fine. it's initialized
     
  7. Anyway, show the full error log please. Also for removing something from a collection there's no need to check if it contains the value, you can just use remove()
     
  8. I've explained the error. I've done debugging, the target player is null, the lines which are printing null are because the target player is null. I'm looking for help on why the target is null.
     
  9. Strahan

    Benefactor

    Well, is the target player online? Was the name spelled right? As mentioned prior, your subcommands are going to be called regardless of if target is null or not. Your validation doesn't enclose them.

    You also should return after the !sender instanceof Player check, it would prevent having to do it yet again right after. Also your target null check should be positive, with an error and return to prevent continued operation if target is null.

    PS you really shouldn't make compound statements either when they rely on data that could be invalid. You also should consider abstraction.
     
  10. How would I go about adding my sub command into the target check since it'll make the commands not work surely since it's not proper indentation with the args.length up above?
     
  11. Strahan

    Benefactor

    As I mentioned. Do a positive null check on target and return if it's invalid. Do it before any operations that rely on target.
     
  12. I changed it around and now my sub-commands won't fire. I'm curious to know what I'm doing wrong?
    Code (Java):
    if (cmd.getName().equalsIgnoreCase("heal") && sender.hasPermission("blue.para.heal")) {
                    if (args.length == 0) {
                        p.sendMessage(Methods.color(BluePara.getInstance().getConfig().getString("Usage")));
                        return true;
                    }
                    if (args.length == 1) {
                        target = Bukkit.getPlayer(args[0]);

                        if (target == null) {
                            p.sendMessage("not online");
                            return false;
                        }
                        //todo: Add this back. Simply for testing on myself
                        /*if (targetPlayer == sender) {
                            p.sendMessage(Methods.color(BluePara.getInstance().getConfig().getString("CannotHealSelf")));
                            return false;
                        }*/

                        if (!BluePara.getInstance().playersAcceptDeny.contains(target.getUniqueId())) {
                            p.sendMessage("hi");
                            p.sendMessage(Methods.color(BluePara.getInstance().getConfig().getString("SentHeal").replace("%player%", target.getName())));
                            BluePara.getInstance().playersAcceptDeny.add(target.getUniqueId());
                            Methods.hoverRunCmd(target, BluePara.getInstance().getConfig().getString("Accept").replace("%player%", p.getName()).replace("%price%", Double.toString(price)), "/heal accept", BluePara.getInstance().getConfig().getString("Accept").replace("%player%", p.getName()));
                            Methods.hoverRunCmd(target, BluePara.getInstance().getConfig().getString("Deny").replace("%player%", p.getName()), "/heal deny", BluePara.getInstance().getConfig().getString("Deny").replace("%player%", p.getName()));
                        }
                        else if (args[0].equalsIgnoreCase("accept")) {
                            Bukkit.broadcastMessage("uuid: " + target.getUniqueId());
                            if (BluePara.getInstance().playersAcceptDeny.contains(target.getUniqueId())) {
                                p.sendMessage("test");
                                target.sendMessage(Methods.color(BluePara.getInstance().getConfig().getString("AcceptHeal").replace("%player%", p.getName()).replace("%price%", Double.toString(price))));
                                p.sendMessage(Methods.color(BluePara.getInstance().getConfig().getString("TargetAccepted").replace("%player%", target.getName())));
                            }
                        }
                        else if (args[0].equalsIgnoreCase("deny")) {
                            if (BluePara.getInstance().playersAcceptDeny.remove(target.getUniqueId())) {
                                Bukkit.broadcastMessage("removing");
                            }

                        }
     
  13. Code (Text):
    if (target == null) {
         p.sendMessage("not online");
         return false;
    }
    return true;

    Edit: this is again because your target is null.
     
  14. I’m unsure on how the target is null when the typed command is me? It’s displaying correctly on chat just not checking the ArrayList correctly?
     
  15. You're making it so target is the first argument, but then proceed to check if the first argument is equal to accept or deny. I'm guessing since an invalid player is typed for args[0], you meant to make a command like /heal accept [player]. In this case, you would keep the accept/deny at args[0], but use args[1] for the target. Now, if this is the case, you should probably create a check to make sure the args length is 2.
     
  16. I'm basically trying to do these commands;
    /Heal - Help (Args 0)
    /Heal <player> (Args 1)
    /Heal accept (Args 1 again)
    /Heal deny (Args 1 again)

    but I can't seem to figure out how to keep the same args while not interfering with heal accept / deny.
     
  17. Code (Java):
    if (args[0].equalsIgnoreCase("accept")) {

    } else if (args[0].equalsIgnoreCase("deny")) {

    } else {
        // look for player name
    }
    Edit: if you want to enable the ability to heal a player named "accept" (which is a great name btw), you could introduce something like /heal player:<player_name>. In that case:

    Code (Java):
    if (args[0].beginsWith("player:")) {
        String playerName = args[0].replaceFirst("player:", "");
        // look for player
    }
    if (args[0].equalsIgnoreCase("accept")) {

    } else if (args[0].equalsIgnoreCase("deny")) {

    } else {
        // look for player name
    }
     
     
    #17 Schottky, Feb 2, 2020
    Last edited: Feb 2, 2020
    • Like Like x 1
    • Winner Winner x 1
Thread Status:
Not open for further replies.