nullPointerException, but why? D:

Discussion in 'Plugin Help/Development/Requests' started by ImAFlo, Jun 3, 2015.

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

    ImAFlo

    Hay.

    I get a nullpointer exception in line 567 in my plugin
    The line should get the item id of the item in a hotbar slot. In the hotbar slot is an item.
    Eclipse shows no errors, console error
    Console Error (open)

    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'mhub-edit-teleporter' in plugin Modern_Hub v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-5ffe41c-bd703b8]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-5ffe41c-bd703b8]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot.jar:git-Spigot-5ffe41c-bd703b8]
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot.jar:git-Spigot-5ffe41c-bd703b8]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot.jar:git-Spigot-5ffe41c-bd703b8]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-5ffe41c-bd703b8]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-5ffe41c-bd703b8]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-5ffe41c-bd703b8]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_25]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_25]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:683) [spigot.jar:git-Spigot-5ffe41c-bd703b8]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-5ffe41c-bd703b8]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:623) [spigot.jar:git-Spigot-5ffe41c-bd703b8]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:526) [spigot.jar:git-Spigot-5ffe41c-bd703b8]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_25]
    Caused by: java.lang.NullPointerException
        at me.flo.hub.Hub.saveInvItem(Hub.java:567) ~[?:?]
        at me.flo.hub.Hub.onCommand(Hub.java:287) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-5ffe41c-bd703b8]
        ... 14 more


    Code: http://pastebin.com/ZKRVadKD

    Plz help

    Thanks in Advance
     
  2. You first need to check if item.hasItemMeta()
     
  3. Offline

    ImAFlo

    @FisheyLP Sorry, in the pastebin post is it not line 567 but line 566. (p.getInventory().getItem(SlotNum).getTypeId())
     
  4. The item can be null
     
  5. Offline

    ImAFlo

    But it is null.
     
  6. Offline

    stormneo7

    So how do you get the ID of a null object?
     
  7. Offline

    ImAFlo

    @stormneo7
    Yea.
    Or its the db.set() function, but I don't know why D:
     
    Last edited: Jun 3, 2015
  8. Offline

    Zombie_Striker

    @ImAFlo
    Make sure everything is not null. Make sure the player is not null, the inventory is not null, the item is not null, ect.
     
  9. Offline

    ImAFlo

    The Player and the Inventory aren't null.
     
  10. But the item at the slot is null
     
  11. Offline

    ImAFlo

    The Itemslot contains a Item.
     
  12. Offline

    Zombie_Striker

    @ImAFlo
    Add null checks for the item. Again, NULL TEST EVERYTHING!
     
  13. Offline

    ImAFlo

    @Zombie_Striker
    Now I tested everything and now I know, the item (p.getInventory().getItem(SlotNum)) is null.
    But I have items in inventory slot 0-7 (1-8 hotbar slot.)
    #push
     
  14. Offline

    Gamesareme

    @ImAFlo In your code, you get the item in slot 0. Is that meant to be slot 1? There is not slot 0 in the hotbar.
     
  15. @Gamesareme
    Wrong. The hotbar slots are working like indexes: 0-8
     
  16. Offline

    ImAFlo

    @Gamesareme Java begins counting by 0, (slot 0 = slot 1, slot 1 = slot 2)
    @FisheyLP Yeah, but I only need slot 0-7 :D
     
  17. Offline

    Zombie_Striker

    @ImAFlo
    Can you show us what you used to test if the slots were null? Actually seeing what you did might help us.
     
  18. Offline

    ImAFlo

    I testet: if (p.getInventory().getItem(Slotnum) == null) { p.sendMessage("item ist null") }
    The same I did with Inventory, getType and get String of the db config.
     
  19. Offline

    Gamesareme

    @ImAFlo This works for me. Not sure how you checked for null.
    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInateracte(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
                for(int i = 0; i <= 7; i++){
                    if(player.getInventory().getItem(i) != null){
                        player.sendMessage("Item " + (i) + " is " + player.getInventory().getItem(i).getTypeId());
                    }else{
                        player.sendMessage("Item " + (i) + " is air");
                    }
                }
            }
        }
    This shows how I went about getting the id of the material in the players hotbar.
    May I also suggest you use for loops for your saving as well.
    It would be something like this.
    Code:
    for(int i = 0; i <=7; i+){
    saveInvItem(p, "s" + i, i);
    }
    This is a better use of code, than just copy paste.
    :)
     
Thread Status:
Not open for further replies.

Share This Page