Solved Removing Permissions

Discussion in 'Spigot Plugin Development' started by DisCoder212, May 30, 2018.

  1. I have a class where I add and remove player permissions, but for some reason when I try to get rid of a permission a player has then it throws off an error -

    My Methods:
    Code (Text):


    private HashMap<UUID, PermissionAttachment> pPermissions = new HashMap<UUID, PermissionAttachment>();

    public void setupPermisisons(Player player) {

        String group = mySQL.getGroup(player.getUniqueId());

        PermissionAttachment attachment = player.addAttachment(plugin);

        pPermissions.put(player.getUniqueId(), attachment);

        for (String groups : config.getConfigurationSection("Groups").getKeys(false)) {
            for (String permissions : config.getStringList("Groups." + group.toLowerCase() + ".Permissions")) {
                attachment.setPermission(permissions, true);
            }
        }
    }

    public void unsetPermissions(Player player, final String permission) {
        pPermissions.get(player.getUniqueId()).setPermission(permission, false); --> This line is the one causing the NPE
    }
     
    The error:
    Code (Text):

    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin GPerms v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
        at java.base/java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at java.base/java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.NullPointerException
     
     
  2. Do you understand how to read StackTraces (Just asking so I can better help you)

    Also there should be more after the Caused By that will identify the file and line in code that it says is causing the NPE (Null Pointer Exception), can you post the full StackTrace so I can get a better idea of whats going on and what its warning about?
     
    • Friendly Friendly x 1
  3. Yes I can, sorry for the late reply I must of miss-copied the stack trace my bad!

    -
    Code (Text):

    [19:36:28] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin GPerms v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
        at java.base/java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        at java.base/java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.NullPointerException
        at me.discoder.gperms.utils.GManager.unsetPermissions(GManager.java:133) ~[?:?]
        at me.discoder.gperms.GPerms.onCommand(GPerms.java:49) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot1.8.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
     

    Line 133 in GManager is the following:
    Code (Text):

    pPermissions.get(player.getUniqueId()).setPermission(permission, false);
     

    Line 49 in GPerms is just me using that method to remove a player's permissions -
    Code (Text):

    gManager.unsetPermissions(player, "permission.test");
     
     
  4. There are two options. pPermission does not contain your player's uuid or the player is null. I would check for if pPermission.contains(...) before because I doubt your player is null.
     
  5. Yeah weird it was because the player wasn't in the HashMap. The weird part is when the player joins I put them in the HashMap for some reason it only worked was because I put the HashMap in the Main class it wouldn't work if it wasn't in the main class, by any chance do you know why it only works if the HashMap is within the Main class?
     
  6. Maybe because you are using two different instances of the class holding the hashmap?