Solved NullPointerException and not a clue in the world

Discussion in 'Spigot Plugin Development' started by Oscazz, May 27, 2016.

  1. Hey guys, i've been working on a plugin that is an all-around combat plugin, and i've been having this problem.
    Basically, i've been trying to detect when someone's Boolean is true when they chat, and if it is, proceed and execute the code.
    Here is my code:
    Code (Text):
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
            if (!setnottaggedmessage.get(event.getPlayer().getUniqueId().equals(true))) {
                return;
            }
            getLogger().info("Set cancelled called!");
            event.setCancelled(true);
            nottaggedmessage = event.getMessage();
            setnottaggedmessage.put(event.getPlayer().getUniqueId(), false);
        }
    It is trying to pull it from this map:
    Code (Text):
        private Map<UUID, Boolean> setnottaggedmessage = new HashMap<>();
    But i'm getting a NullPointerException on line 313, the if (!setnottaggedmessage) line.
    IntelliJ is saying that the statement might not contain objects of a Boolean.
    Here is where I set their Boolean to true:
    Code (Text):
        @EventHandler
        public void onClick(InventoryClickEvent event) {
            Inventory inventory = event.getClickedInventory();
            if (inventory.getName().equalsIgnoreCase(ChatColor.DARK_GRAY + "Killsplus - Combat Tag Config")) {
                event.setCancelled(true);
                Player player = (Player) event.getWhoClicked();
                if (event.getSlot() == 3) {
                    player.closeInventory();
                    settaggedmessage.put(event.getWhoClicked().getUniqueId(), true);
                    player.sendMessage(ChatColor.BLUE + "Type your 'No Longer Tagged' Message in the chat!");
                    player.sendMessage(ChatColor.BLUE + "Remember, %player% will be replaced with the player name and you can use colour-codes too!");
    And here is the error i get:
    Code (Text):
    [07:21:50] [Server thread/INFO]: _FireNinja issued server command: /kps config
    [07:21:52] [Async Chat Thread - #0/ERROR]: Could not pass event AsyncPlayerChatEvent to KillsPlus v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot-1.8.jar:git-Spigot-d0d1d87-bc03b6f]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.jar:git-Spigot-d0d1d87-bc03b6f]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.jar:git-Spigot-d0d1d87-bc03b6f]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.8.jar:git-Spigot-d0d1d87-bc03b6f]
        at net.minecraft.server.v1_8_R1.PlayerConnection.chat(PlayerConnection.java:1036) [spigot-1.8.jar:git-Spigot-d0d1d87-bc03b6f]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:975) [spigot-1.8.jar:git-Spigot-d0d1d87-bc03b6f]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot-1.8.jar:git-Spigot-d0d1d87-bc03b6f]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat$1.run(PacketPlayInChat.java:47) [spigot-1.8.jar:git-Spigot-d0d1d87-bc03b6f]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_45]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at me.fireninja.killsplus.Main.onChat(Main.java:313) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot-1.8.jar:git-Spigot-d0d1d87-bc03b6f]
        ... 12 more
    [07:21:52] [Async Chat Thread - #0/INFO]: <_FireNinja> &a%player% you are no longer tagged! &9HOORAY&a![m
    At this point I am completely lost, I need to figure out how to detect the Boolean some other way is what I think I need to do.
    Does anyone know how they could help me?
    Thanks,
    -FireNinja
     
  2. if(!setnottaggedmessage.get(event.getPlayer().getUniqueId(), true)), as in tour current check there is no boolean check
     
  3. For boolean objects you should use the Boolean#.booleanValue() method. As far as the null pointer, you should probably add a null check somewhere to help debug.
     
  4. This
    Code (Text):
    if (!setnottaggedmessage.get(event.getPlayer().getUniqueId().equals(true))) {
    is wrong. Change to:
    Code (Text):
    if (!setnottaggedmessage.get(event.getPlayer().getUniqueId())) {
    EDIT: Just realized you wanted to check if its true? In that case remove the !
     
  5. No, I want to cancel if it is false, your solution didn't work either, same error ;-;
    Will try adding a null detector now
     
  6. That's why I added the edit :p just remove the exclamation point. As for the NPE I didn't even consider it, but your if condition as it was simply would never work.
     
  7. Switched my code to this:
    Code (Text):
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
            if (!setnottaggedmessage.get(event.getPlayer().getUniqueId().equals(null))) {
                getLogger().info("Null method called!");
                return;
            }
            if (!setnottaggedmessage.get(event.getPlayer().getUniqueId())) {
                getLogger().info("False method called!");
                return;
            }
            if (setnottaggedmessage.get(event.getPlayer().getUniqueId())) {
                getLogger().info("Set cancelled called!");
                event.setCancelled(true);
                nottaggedmessage = event.getMessage();
                setnottaggedmessage.put(event.getPlayer().getUniqueId(), false);
            }
            return;
        }
    And still i'm getting the same error, P.S I know there is a || function, not using for debug rn :)

    EDIT: Also, not a single logger did anything in the console :_:
     
  8. Just from a glance the only thing I can is that "setnottaggedmessage" is somehow null at point the event fires.
     
  9. NVM ... get rid of this...
    Code (Text):

            if (!setnottaggedmessage.get(event.getPlayer().getUniqueId().equals(null))) {
                getLogger().info("Null method called!");
                return;
            }
     
    It does nothing and is probably causing your NPE
     
  10. Ok, I just tried that, but sadly still the exact same result... :(
     
  11. In that case I do believe setnottaggedmessage is null.

    EDIT: Can you show the full code?
     
  12. Ok, I will PM it to you :)
     
  13. PM'd you
     
  14. Thanks everyone for helping, but I realized what the problem was and fixed it... It was a silly typing mistake xD
    In the clicking slot 3 in my GUI it would set the STRING to the uuid and true, not the Map... XD
    Anyway, thanks everyone for helping as much as you could, it's really appreciated :)