Solved Arraylist UUID

Discussion in 'Spigot Plugin Development' started by 1Mangomaster1, Oct 12, 2019.

  1. I have an ArrayList that uses UUID's to contain players... I wanted to check if the player is not within this ArrayList so I used this:

    Code (Java):
    if(!Main.getInstance().Chosen1.contains(p.getUniqueId())){
    //do something
    return true; //boolean action
    } else {
    return true;
    }

    it showed me an error says:

    Ik what this error means, but I just have no idea why would it be null.. I did the get instance and the Arraylist correct, there shouldn't be a reason for it to be like that.. not one I can see at least.. ;-;

    appreciate every comment :)
     
  2. SteelPhoenix

    Junior Mod

    Apparently you didn't or the exception wouldn't be thrown. You need to show more of your code.

    Also please name stuff according to Java's naming convention and don't call your main class "Main". Also there's no reason for public field access, so don't.
     
  3. Code (Java):
        public Main plugin;

        public Element(Main main) {
            plugin = main;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (label.equalsIgnoreCase("element") || label.equalsIgnoreCase("e")) {
                if (sender instanceof Player) {
                    Player p = (Player) sender;
                    if (args.length == 0) {
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&fUse: &b/element help"));
                        return true;
                    }
                    if (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("h")) {
                        new HelpCommand(p, label, args);
                        return true;
                    }
                    if (args[0].equalsIgnoreCase("choose") || args[0].equalsIgnoreCase("c")) {
                        if (!Main.getInstance().Chosen1.contains(p.getUniqueId())) {
                            new ChooseCommand(p, label, args);
                            return true;
                        } else {
                            return true;
                        }
                    }
                } else {
                    sender.sendMessage(ChatColor.DARK_RED + "Only players may execute this command!");
                    return true;
                }
            }
            return false;
        }
    this is the code inside the class
    if I don't use the IF I showed before it does work, but this if is critical

    NOTE:

    Btw, this is the instance and the arraylist code inside the main class

    Code (Java):
    private static Main main;
    public ArrayList<UUID> Chosen1 = new ArrayList<>();

    public static Main getInstance() {
            return main;
        }
    And also, I changed the names of Main and Element because I noticed Element is also a captured word sorta..
     
    #3 1Mangomaster1, Oct 12, 2019
    Last edited: Oct 12, 2019
  4. Gadse

    Gadse Previously GummiBoat

    Why are you getting an instance of your plugin, if you already pass it on to the event? Also SteelPhoenix is right, you should use private variables and public methods to get/set them. For Lists you might even want to consider making methods specifically to add/remove players.

    Demonstration:
    Code (Java):
    private List<UUID> players = new ArrayList<>();
    public List<UUID> getPlayers() { return players; }
    public void addPlayer(UUID uuid) { players.add(uuid); }
    public void removePlayer(UUID uuid) { players.remove(uuid); }
    Then, inside your command, it is sufficient to use
    Code (Java):
    if(plugin.getPlayers().contains(player.getUniqueId())) { /* do stuff */ }
    But since you did not provide the full stack trace, I'm not even sure that the List is the issue.
     
    • Winner Winner x 1
  5. The thing is, that I want the ArrayList to be the same in all classes, not just in that specific class..
    like, if I update the value of this ArrayList in one class, then check it in another class, the other class would be aware of the update.. weird to explain

    AH ok nvm I am stupid, ;-;

    NOTE:

    Stack trace?? wdym? like.. the whole error?
    I can do that
     
    #5 1Mangomaster1, Oct 12, 2019
    Last edited: Oct 12, 2019
  6.  
  7. What you gave me did work.. perhaps I did get the ArrayList wrong, weird tho, I did the same on another project but instead of UUID with Player name, (Today I thought UUID would be better but I didn't think of it then) and it did work... But thanks anyways :)
     
  8. Gadse

    Gadse Previously GummiBoat

    The issue was probably Main.getInstance(), maybe you forgot to set main to an actual value in onEnable(). But, it is also perfectly fine to pass on the plugin in the constructors, so you might not need to make an instance at all.
     
  9. Yeah.. I guess I was kinda stupid with it lol... Thanks a lot