NullPointerException ._.

Discussion in 'Plugin Development' started by rockers3000, Jul 16, 2015.

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

    rockers3000

    Well, I've been a little stuck on this and I'm not aware of a way to fix it. But then again, I'm new to Bukkit.

    Code:
                            
    //Player clicked bottom block
                            player.sendMessage(player.getItemInHand().getItemMeta().getLore().get(1));
                            int id = Integer.parseInt(player.getItemInHand().getItemMeta().getLore().get(1));
                            ArrayList<Location> listLocation = new ArrayList<Location>();
                            try
                            {
                                listLocation = jailDoorIDHash.get(id);
                            }
                            catch(IndexOutOfBoundsException e){}
                            listLocation.add(b.getLocation());
                            jailDoorIDHash.put(id, listLocation);
    
    This basically gets a HashMap<int, ArrayList<Location>> and gets its value (ArrayList<Location>) based off of player.getItemInHand().getItemMeta().getLore().get(1) parsed. Up to there, it works (seemingly). However, when it reads listLocation.add(b.getLocation()), I get a NullPointerException...

    Code:
    [01:50:01] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to JailbreakPlugin v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:210) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:462) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:705) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_31]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:672) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:628) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:536) [craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.NullPointerException
        at me.LyesDoesPlugins.jailbreakPlugin.jailbreakMain.onPlayerUse(jailbreakMain.java:155) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_31]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit-1.8.7-R0.1-SNAPSHOT-remapped.jar:git-Bukkit-bf4818b]
        ... 17 more
    
    Could someone help?
     
  2. Offline

    mariosunny

    The problem is b is null. But I have no idea what b is, where it is initialized, or how it is mutated throughout its lifecycle. I cannot solve your problem without this information.

    Also, I have no idea why you are using a try/catch block in that function. Can't you just check if jailDoorIDHash contains id?
     
  3. Offline

    rockers3000

    Sorry, I forgot to mention that b is a Block. I have also tried it with other locations, however, and have still gotten the same error. I also was using the try-catch mostly for debugging, but I don't think thats what gave me the error. I could be wrong, but I don't think thats the problem. (Im not on my PC at the moment so I cant check.)
     
  4. Offline

    Shortninja66

    I really can't tell what you're doing exactly, but you should check if the item has item meta before getting the lore.
     
  5. Offline

    rockers3000

    Before all of this, it checks to see if the player is holding a specific tool with a specific lore set at lore(0). When you get the tool, you then must set an ID which gets set to lore(1). When you right click with this tool on an iron door, it should add the iron door's bottom block postion to the jailDoorIDHash value with the key being the tool ID (lore1). Example:

    Player types command to get tool. (with args [0] being the ID)

    Player recieves special tool (With lore0 being set to "JailDoorWandTool" or something and lore1 being set to args [0], The ID)

    Player clicks iron door.
    This *should* add the bottom blocks location to a Location-ArrayList within the HashMap based off of the tool's ID (lore1).

    It is all working up till
    listLocation.add(b.getLocation());

    Tommorow, I will try to post the code, if you can't figure it out.
    (Its 3.30 AM where I live.)
     
  6. Offline

    teej107

    Shortninja66 likes this.
  7. Offline

    Shortninja66

    Well, didn't notice you added a stack trace. Check line 155 in your onPlayerUse listener (at me.LyesDoesPlugins.jailbreakPlugin.jailbreakMain.onPlayerUse(jailbreakMain.java:155).

    Where you said "*should*": Don't believe that your code should work the way you want it to just because you think it will.
     
  8. Offline

    rockers3000

    listLocation.add(b.getLocation()); is line 155.
     
  9. Offline

    teej107

    Given from what I said, that means there are two objects that could be null. What do you think they are?
     
  10. Offline

    rockers3000

    b doesn't evolve or change in any way. It is called and set to an existing block.

    It has to be either the block or listLocation. But I don't think it can be list location, because I set it to be blank, but not null.

    Oh wait. Im guessing this is my problem:
    listLocation = jailDoorIDHash.get(id);
    While I do set listLocation to be empty, not null, before this method, I set it to jailDoorIDHash.get(id).
    Because jailDoorIDHash.get(id) doesn't get set anywhere previously, it would be null, im guessing? A simple if statement would fix this if this was the case.

    Maybe I should have worded it: "My intentions were" :p
     
    Last edited by a moderator: Jul 16, 2015
  11. Offline

    mythbusterma

    @rockers3000

    You can print those objects to the console to see which is null.
     
  12. Offline

    rockers3000

    Using a simple if statement worked more or less, I just tweaked a few things and made it send messages to the player if everything worked out and it seems to be working. Thanks everyone!
     
Thread Status:
Not open for further replies.

Share This Page