Solved Trouble accessing variable from another method

Discussion in 'Spigot Plugin Development' started by devJordan, Dec 10, 2021.

Thread Status:
Not open for further replies.
  1. Hello,

    I am having some issues accessing a variable from another method, this is more of a Java question than anything, but everywhere I research they say just put the variable outside of your methods, that's what I've done but it doesn't seem to be working, here is the code that is relevant:

    I've defined the variable at the top of my class
    Code (Text):
    public final class Main extends JavaPlugin implements Listener {
        Player selected;
    Then I select a random online player (that doesn't have a certain permission) and set that player to "selected". This following code is placed inside a PlayerInteractEvent
    Code (Text):
                // Put all online players (excluding those with the bypass permission) into a list
                ArrayList<Player> allPlayers = new ArrayList<Player>();
                for (Player players : Bukkit.getOnlinePlayers()) {
                    if (!(players.hasPermission("tracker.bypass"))) {
                // If our list equals 0 meaning there are no players online that don't bypass tracker send the player a message
                if (allPlayers.size() == 0) {
                    event.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(net.md_5.bungee.api.ChatColor.RED + "There are no trackable players online"));
                // Finally, get a random player from our list of trackable players
                int randomPlayer = new Random().nextInt(allPlayers.size());
                Player selected = allPlayers.get(randomPlayer);
    The final part is that I want to access the variable in another method, a PlayerDeathEvent to be able to see if the player dies that was selected
    Code (Text):
        // When the tracking player dies stop tracking them
        public void onDeath(PlayerDeathEvent event) {
            System.out.println("Death event seen. Entity that died is: " + event.getEntity());
            System.out.println("Playing being tracked is: " + selected);
            if (event.getEntity() != selected) return;
            System.out.println("Tracker should be set to false");
            boolean trackerActive = false;
    As you can see I have some logging messages going on for debugging, and when I print out the variable "selected" it is null.
  2. You seemingly never assign your selected player to your global variable.
  3. It should be the final line of the second snippet of code?
    Code (Text):
                Player selected = allPlayers.get(randomPlayer);
  4. You're declaring a new 'selected' variable within the scope of your method. It just needs to be this:
    Code (Java):
    selected = allPlayers.get(randomPlayer);
  5. You're creating a new local variable in your method, it is only accessible in your method. You have to initialize your field in order to use it between methods. This might help explain a bit more.
  6. My goodness, thank you so much, that's so dumb of me, I took a short (week or so break from coding) and between that and learning BukkitRunnables for the first time my brain is fried, thank you!
Thread Status:
Not open for further replies.