NullPointer

Discussion in 'Plugin Development' started by SantaClawz69, Aug 16, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    SantaClawz69

    So I'm getting null exception points, and my commands aren't working. Only commands that work are my Promote and Demote command. Someone please help.


    Logs: In here my /wanted commands don't do anything, and my /duty command gives me an error.
    Code:
    [00:50:33] [Server thread/INFO]: SantaClawz416 issued server command: /duty
    [00:50:33] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'duty' in plugin Police v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1351) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1186) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_92]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_92]
        at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_92]
    Caused by: java.lang.NullPointerException
        at org.bukkit.craftbukkit.v1_10_R1.inventory.CraftInventory.setContents(CraftInventory.java:74) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at me.bryan.Police.Main.loadInventory(Main.java:64) ~[?:?]
        at me.bryan.Police.Commands.Duty.onCommand(Duty.java:31) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        ... 15 more
    [00:51:27] [Server thread/INFO]: SantaClawz416 issued server command: /gamemode s
    [00:51:27] [Server thread/INFO]: [SantaClawz416: Set own game mode to Survival Mode]
    [00:51:34] [Server thread/INFO]: SantaClawz416 issued server command: /duty
    [00:51:34] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'duty' in plugin Police v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1351) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1186) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_92]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_92]
        at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_92]
    Caused by: java.lang.NullPointerException
        at org.bukkit.craftbukkit.v1_10_R1.inventory.CraftInventory.setContents(CraftInventory.java:74) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        at me.bryan.Police.Main.loadInventory(Main.java:64) ~[?:?]
        at me.bryan.Police.Commands.Duty.onCommand(Duty.java:31) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
        ... 15 more
    [00:51:38] [Server thread/INFO]: SantaClawz416 issued server command: /gamemode c
    [00:51:38] [Server thread/INFO]: [SantaClawz416: Set own game mode to Creative Mode]
    [00:51:52] [Server thread/INFO]: SantaClawz416 issued server command: /police help
    [00:51:59] [Server thread/INFO]: SantaClawz416 issued server command: /time
    [00:52:02] [Server thread/INFO]: SantaClawz416 issued server command: /time set day
    [00:52:02] [Server thread/INFO]: [SantaClawz416: Set the time to 1000]
    [00:52:11] [Server thread/INFO]: SantaClawz416 issued server command: /gamemode s
    [00:52:11] [Server thread/INFO]: [SantaClawz416: Set own game mode to Survival Mode]
    [00:52:12] [Server thread/INFO]: SantaClawz416 issued server command: /god
    [00:52:20] [Server thread/INFO]: SantaClawz416 issued server command: /gamemode c
    [00:52:20] [Server thread/INFO]: [SantaClawz416: Set own game mode to Creative Mode]
    [00:52:30] [Server thread/INFO]: SantaClawz416 issued server command: /gamemode s
    [00:52:30] [Server thread/INFO]: [SantaClawz416: Set own game mode to Survival Mode]
    [00:52:34] [Server thread/INFO]: SantaClawz416 issued server command: /promote
    [00:52:40] [Server thread/INFO]: SantaClawz416 issued server command: /promote SantaClawz416
    [00:53:13] [Server thread/INFO]: SantaClawz416 issued server command: /wanted
    [00:53:21] [Server thread/INFO]: SantaClawz416 issued server command: /wanted list
    [00:53:32] [Server thread/INFO]: SantaClawz416 issued server command: /wanted set SantaClawz416
    [00:53:34] [Server thread/INFO]: SantaClawz416 issued server command: /wanted list
    [00:54:14] [Server thread/INFO]: SantaClawz416 issued server command: /wanted set
    My source code: https://github.com/BryanBarren/Police
     
  2. Offline

    Zombie_Striker

    This line is causing the NPE:
    Code:
    p.getInventory().setContents((ItemStack[]) inventoryContents.get(p.getName()));
    The only value that can be null is the value returned by that hashmap can be null. What does that value return? Are you sure the player is even in the Map?
     
  3. Offline

    SantaClawz69

    Well I mean when the player runs the command /duty it will do the enterPolice() method. Which will had the player to the hashmap. So why wouldn't he be in it?
     
  4. Offline

    Lordloss

    You call
    Code:
    if (Main.getPoliceMode().contains(player)) {
    on a List<String> which cannot be true because player is a Player. Because this the if is false and
    Code:
    Main.loadInventory(player);
    gets called. Then it trys to get the content for the player in inventoryContents, which returns null. Btw. you dont have to cast all this stuff to ItemStack[] because you use generic collections. Take a look at generics. And you dont have to wrap ItemStacks in an Array if you want to add it to an Inventory, just add it directly.

    EDIT: Your Wanted command does nothing, because it checks if the player is in policeMode, which he cannot be because of the broken if statement i mentioned above.

    thank you for posting all your source, this makes it easy to spot the issues and is very commendable in such cases.
     
    Last edited: Aug 17, 2016
  5. Offline

    SantaClawz69

    Ah yes, I changed the arraylist problem to .getName() but I'm still getting this error. It's really bothering me and I've been staying up all night 4:15AM as we speak. This is very saddening -.-
     
  6. Offline

    Lordloss

    Okay i think its time for you to make some debug messages. In front of the if checks print out if policemode contains the player, make some debug messages in the method where the player should get added and try to figure out why he is not.
     
  7. Offline

    timtower Administrator Administrator Moderator

    @SantaClawz69 Get some sleep first, then continue with this, most people are worthless when without sleep.
     
  8. Offline

    SantaClawz69

    I think I'm going to take your advice.

    And trust me, I've been editing the code non-stop putting in my debugging messages. It's just not coming to me. If you find out the reason please let me know. I'll be back in the morning.

    EDIT: Also if you find anything else wrong with my plugin, please put it in so I can edit it.
     
    timtower likes this.
  9. Offline

    Lordloss

    I allready told you some small issues ;). One thing i can offer you at the moment is that i put all your code in my IDE and try a bit around by myself. Im pretty sure that the players just dont get added for unknown reasons, i expect some logical errors. I can do this as soon i get home from work.
     
  10. Offline

    SantaClawz69

    Thanks, I really appreciate it.

    If someone else can help me with my problem that would be great to!
    @Zombie_Striker
     
    Last edited: Aug 17, 2016
  11. Offline

    Zombie_Striker

    @SantaClawz69
    If you are still getting your error, the reason why is because the player is not in the Map. It looks like "enterPolice" is the method that should save the player's inventory.
    Code:
    if (Main.getPoliceMode().contains(player)) {
                        Main.enterPolice(player);
                        player.sendMessage("working");
                    } else {
                        Main.loadInventory(player);
                        player.sendMessage("working");
    }
    Your issue As Lord has pointed out, the if statement has unreachable code. One of the lines of unreachable code is where you actually save your inventory. This means the inventory is never saved, and as such the Map does not contain the player's name. Add ".getName()" to the end of that if statement, and try issuing out the command again. That should fix your problem.
     
  12. Offline

    SantaClawz69

    Forgot to mention, I already put the .getName() and I'm still getting the same errors for some reason. Would you like me to update my code?

    It seems as if my loadInventory() is giving me the problem here. On the hashmap of Inventory COntents line. I'm not too sure as of why @Zombie_Striker
     
  13. Offline

    Zombie_Striker

    @SantaClawz69
    Here's the thing; the object in the hashmap is null. You can debug it if you want to make sure. For some reason, the player's contents are not being added to the map. What needs to be done now is to debug the whole chain of methods. If you want an ordered list, here is what you need to do:
    1. Check if "saveInventory" ever gets called.
    2. If it does get called, check if/why the items are not being stored in the map
    3. If it does not get called, go up a level (to "enterPolice") and repeat the process for that method.
     
  14. Offline

    SantaClawz69

    Got it to work, problem was that getPoliceMode() was called in the if. But how could the player be in PoliceMode if was never added. Looks like you guys were absolutely right. So instead I just made a new hashmap that saves all players names and a string to see if their police. And if their in that hashmap then everything gets enabled. Thanks for your help!

    But sadly my items aren't working. So if you could have a look into that for me? That would be nice.
     
  15. Offline

    Zombie_Striker

    @SantaClawz69
    Which items are not working (or which lines for the items are not working), and how are they not working? Are there any errors?
     
  16. Offline

    SantaClawz69

    My Tazer isn't working, and no errors.
     
  17. Offline

    Zombie_Striker

    This is really important to know, because I need to know where to look. I'll still look, but I may not find the exact issue.

    First you are not null checking the item. Second, unless the player holds the "tazer" in his hand the entire time, there is a possibility that the item may not be a tazer. Finally, the damage level given to the player is well over the max amount of health an entity can have (1028). These will cause problems later down the line.

    Have you tried debugging? Do you know which method/function is not working?
     
  18. Offline

    SantaClawz69

    I'm sorry what do you mean by this?
    I'll do this now.

    EDIT: None of my items are working, literally nothing. I did some debugging with player messages and whenever I use the item it doesn't work. I updated my code on the github. Please help me out @Zombie_Striker
     
    Last edited: Aug 17, 2016
  19. Offline

    Zombie_Striker

    @SantaClawz69
    This event is triggered when the arrow actually hits the player. That means that the line you have will get the item in the player's hand at the time the arrow hits. Since an arrow takes time to fly towards another player, there is time for one player to change the item in his hand (e.g. Bow is in slot 4. The player shoots the bow, and then goes to slot 5. Slot 5 is not the bow that shot the arrow. It could be the "tazer" that shot the arrow, but that line is checking what the item is now, not what it was when he shot the arrow) Fix this by checking ProjectileLaunchEvent and saving if the arrow was shot by the "tazer"

    Configs should only be used for storing data when the server shuts down. It is better to use the hashmap instead of using the config for cooldowns.

    Since you create and register the item in the main class, the events should be firing. Debug, starting from the first line and going down line-by-line, and see at what point does the taser stop working.

    BTW: It is spelled "Taser" with an 's', because it is an acronym for the “Thomas A. Swift's Electric Rifle.”
     
  20. Offline

    SantaClawz69

    Well I just debugged, and taser doesn't work from the first line. I'm also getting no errors in console so something is clearly wrong and I'm not sure what's so wrong. Everything I've done seems right.
     
  21. Offline

    Lordloss

    What do you mean with
    Do you mean the playerInteractEntityEvent? I see several small/big issues with this event:
    Code:
    if (p.getItemInHand().equals(Main.tazer)) {
    You checked exactly this one if statement higher. Remove the second one.
    Code:
    e.getPlayer()
    You use this dozens of times, although you saved it in the variable p at the beginning. Replace all occurances of this with p, to increase readability.

    Code:
    (int) ((cooldown.get(e.getPlayer()) + 2000)
    You are casting a long which stores large millisecond values to an int, which could eventually make problems.

    The whole event is hard to read, you have a very deep and messy nesting. Try to format the event in a way it is more readable, so errors can be spotted more easily. I would do it, but maybe i get called out because it seems like spoonfeeding :p

    I can give you one example what i do if i have multiple if statements in an event, which just make sure some requirements are met.

    Instead of wrapping all together like you do with:

    Code:
    if (player.has.this && player.has.that) {
        if (player.is.allowed.to) {
            if (player.has.right.item) {
                //actual code which does something
            }
            else {
            player.msg("wrong item");
            }
        }
        else {
        player.msg("you dont.");
        }
    }
    In some situations there are different ways of making it more clear and readable. For events i often use something like this to quick escape from an event and seperate this stuff from the actual code:

    Code:
        if (!(e.getEntity instanceof Player)) return;
        Player p = e.getEntity;
        if (!p.hasThis || !p.hasThat) return;
        if (!p.IsAllowedToDoThis) {
            p.msg("You dont.");
            return;
        }
    
    
        //Here goes the actual code and is nicely seperated from this whole checking stuff.
    Maybe this is not optimal OOP, maybe some people think its not okay, but for me it helped a lot.
    And you can easily put debug messages between them.
    Its a matter of taste, other ways can improve the code also if formatting is well.
     
  22. @SantaClawz69
    Also, there is no need for all these variables and methods to be static. If you need these to be in the main class, just make a method to return the instance of the main class, and from that access those variables and methods.
     
  23. Offline

    SantaClawz69

    I already do have those but i usually set them to static while testing, then once I see the whole method works I fix it up.
    So I found out that instead of PlayerInteractEvent I accidently put PlayerInteractEntityEvent, so I changed that in hopes it would fix it, along with formatting it the way you've suggested and still nothing is working. I don't really understand. @Lordloss I updated the code as well. @AlvinB

    Bump? (Code updated)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 19, 2016
Thread Status:
Not open for further replies.

Share This Page