Solved Getting thrown a null pointer in #hasPermission

Discussion in 'Spigot Plugin Development' started by iCodinqs, May 1, 2017.

  1. So, I am getting thrown a null pointer every time and its starting to drive me insane,

    What the aim is, is basically an automatic staff plugin. More so, this part is the auto removed from the staff list.

    Code what's giving an error:
    Code (Text):
    if (player.hasPermission("group." + group)) {
    The method it's in:
    Code (Text):
    private static String getPlayerGroup(Player player, List<String> possibleGroups) {
            for (String group : possibleGroups) {
                if (player.hasPermission("group." + group)) {
                    return group;
                }
            }
            return null;
        }
    ^ Code above is taken from here

    Basically, this is a way to check see if the user is in the group. If it's not it will give a null pointer. Though, it always gives the null pointer to where the hasPermission is. Thus meaning it doesn't actually return anything because it never completed the full method.

    This method is called in a Bukkit Runnable:
    Code (Text):
    plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {

                public void run() {
                    List<String> staffUsers = plugin.getConfig().getStringList("staffusers");
                    List<String> staffGroups = plugin.getConfig().getStringList("Staff-Groups");

                    for (String player: staffUsers) {
                        Player OPL = Bukkit.getPlayer(player);

                            if (getPlayerGroup(OPL, staffGroups) == null) {

                                if (PlayerData.hasYaml(OPL.getUniqueId())) {
                                    removeUserCache(OPL);
                                    staffUsers.remove(OPL.toString());
                                    plugin.getConfig().set("staffusers", staffUsers);
                                    Bukkit.broadcastMessage("Purging demoted players...");
                                }

                            }

                        }
                    }

            }, 0L, 60*20L);

        }
    the reason why it runs every minute is because I am testing it, it will be moved up of course.

    So, basically, is there another way I can check the player's group or even fix this to have it complete the full method? Because at the moment I am lost.

    StackTrace:
    Code (Text):
    [19:57:49 WARN]: [MDN-Staff] Plugin MDN-Staff v1.0 generated an exception while
    executing task 426
    java.lang.NullPointerException
            at tech.icodinqs.events.Demoter.getPlayerGroup(Demoter.java:64) ~[?:?]
            at tech.icodinqs.events.Demoter$1.run(Demoter.java:39) ~[?:?]
            at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftTask.run(CraftTask.jav
    a:71) ~[spigot-1.11.2.jar:git-Spigot-d276ab1-d219213]
            at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftAsyncTask.run(CraftAsy
    ncTask.java:52) [spigot-1.11.2.jar:git-Spigot-d276ab1-d219213]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:
    1.8.0_121]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?
    :1.8.0_121]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    >
     
  2. What's line 64 of demoted.java?


    Sent from my iPhone using Tapatalk
     
  3. Look at the thread properly, please. I have pasted the error line close to the top.
     
  4. That means that the player (OPL) is null.
    When can be the player null? If "Player OPL = Bukkit.getPlayer(player);" sets OPL to null.
    When does Bukkit#getPlayer(String) return null? If there isn't an online player named that.
     
  5. If player.hasPermission() is throwing a NPE player is null

    hasPermission() can only ever return true or false.
    Sent from my iPhone using Tapatalk
     
  6. You probably need to be getting the offline player if they are written to a config, because you can look up a player but if they're not online the player object will be null


    Sent from my iPhone using Tapatalk
     
  7. True, honestly never thought of it.

    I will have to get the offline player, though, I apparently can't check an offlines players group. Unsure if there are any workarounds for this since LuckPerms API doesn't really provide much information for checking players groups besides the code above.

    EDIT: forgot theres #getPlayer() :p
     
  8. Also, getPlayer "autocompletes" usernames. I believe you should have used getPlayerExact (or getPlayer(UUID) ) in this case.
     
    • Informative Informative x 1
  9. Well, thank you for all for the help, with such a nooby thing that I had forgotten aha.