Solved My plugin is having an error for a vague reason.

Discussion in 'Spigot Plugin Development' started by N0TKingminer7, Jul 6, 2021.

  1. My plugin is erroring when I try to check if an item is a golden sword.
    (Changed title to be more accurate)

    Error:
    Code (Text):
    java.lang.NullPointerException: null
        at com.kingminer7.bedwars.Bedwars.onTick(Bedwars.java:45) ~[?:?]
        at com.kingminer7.bedwars.Bedwars.access$000(Bedwars.java:13) ~[?:?]
        at com.kingminer7.bedwars.Bedwars$1.run(Bedwars.java:25) ~[?:?]
        at org.bukkit.craftbukkit.v1_16_R3.scheduler.CraftTask.run(CraftTask.java:100) ~[patched_1.16.5.jar:git-Paper-778]
        at org.bukkit.craftbukkit.v1_16_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:468) ~[patched_1.16.5.jar:git-Paper-778]
        at net.minecraft.server.v1_16_R3.MinecraftServer.b(MinecraftServer.java:1427) ~[patched_1.16.5.jar:git-Paper-778]
        at net.minecraft.server.v1_16_R3.DedicatedServer.b(DedicatedServer.java:436) ~[patched_1.16.5.jar:git-Paper-778]
        at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1342) ~[patched_1.16.5.jar:git-Paper-778]
        at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:1130) ~[patched_1.16.5.jar:git-Paper-778]
        at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:291) ~[patched_1.16.5.jar:git-Paper-778]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_292]
    Here's my code:
    Code (Text):

    (Line 40)for(Player player : getServer().getOnlinePlayers()) {
    (Line 41)            boolean sharp = false;
    (Line 42)
    (Line 43)            //Loops through for the upgrades first, then loops again for upgradable items.
    (Line 44)            for (ItemStack item : player.getInventory().getContents()) {
    (Line 45)                if(item.getType() != null && item.getType() == Material.GOLDEN_SWORD) {
    (Line 46)                    sharp = true;
    (Line 47)                }
    (Line 48)            }
    (Line 49)
    (Line 50)            for (ItemStack item : player.getInventory().getContents()) {
    (Line 51)                if(item.getType() == Material.WOODEN_SWORD || item.getType() == Material.STONE_SWORD || item.getType() == Material.IRON_SWORD || item.getType() == Material.DIAMOND_SWORD) {
    (Line 52)                    if(sharp = true && !(item.containsEnchantment(Enchantment.DAMAGE_ALL))) {
    (Line 53)                        item.addEnchantment(Enchantment.DAMAGE_ALL,1);
    (Line 54)                    }
    (Line 55)                }
    (Line 56)            }
    (Line 57)        }
     
    Anyone have any idea what's wrong?
     
    #1 N0TKingminer7, Jul 6, 2021
    Last edited: Jul 6, 2021
  2. can you post the full error code?
     
  3. Oops, yeah sorry
    Code (Text):
    [19:52:49] [Server thread/WARN]: [KM7Bedwars] Task #2 for KM7Bedwars v1.0.0 generated an exception
    java.lang.NullPointerException: null
        at com.kingminer7.bedwars.Bedwars.onTick(Bedwars.java:45) ~[?:?]
        at com.kingminer7.bedwars.Bedwars.access$000(Bedwars.java:13) ~[?:?]
        at com.kingminer7.bedwars.Bedwars$1.run(Bedwars.java:25) ~[?:?]
        at org.bukkit.craftbukkit.v1_16_R3.scheduler.CraftTask.run(CraftTask.java:100) ~[patched_1.16.5.jar:git-Paper-778]
        at org.bukkit.craftbukkit.v1_16_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:468) ~[patched_1.16.5.jar:git-Paper-778]
        at net.minecraft.server.v1_16_R3.MinecraftServer.b(MinecraftServer.java:1427) ~[patched_1.16.5.jar:git-Paper-778]
        at net.minecraft.server.v1_16_R3.DedicatedServer.b(DedicatedServer.java:436) ~[patched_1.16.5.jar:git-Paper-778]
        at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1342) ~[patched_1.16.5.jar:git-Paper-778]
        at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:1130) ~[patched_1.16.5.jar:git-Paper-778]
        at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:291) ~[patched_1.16.5.jar:git-Paper-778]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_292]
     
  4. I would try surrounding with a try catch like this...
    Code (Java):
     //Loops through for the upgrades first, then loops again for upgradable items.
    try {
     for (ItemStack item : player.getInventory().getContents()) {
    if(item.getType() != null && item.getType() == Material.GOLDEN_SWORD) {
    sharp = true;
      }
        }
    catch(NullPointerException exception) {}
     
  5. THANK YOU! It works now!
    :)

    I knew i was missing something.
     
  6. I'd suggest you do a item != null check instead of a try catch, because, when the NPE is thrown you leave the for loop, so you don't really check for every item in a player's inventory.
     
    • Like Like x 2
    • Agree Agree x 1
  7. if you add a return statement it will leave the loop otherwise it shouldnt?
     
  8. You can leave a loop by doing return or break, but if you have a for loop inside a try catch and an exception is thrown it will also escape the loop and run the next bit of code
     
  9. Using a try/catch there is a pretty bad bandaid fix. The actual reason for the NPE is because Inventory#getContents has null array entries for empty slots, so you need to check if item != null instead of item.getType() != null.
     
    • Like Like x 1
    • Agree Agree x 1
  10. couldnt you just move the try catch to the if statement (inside the for loop then) I was just pseudo coding to show what a try catch looked like
     
  11. Why do this if you can check such a simple condition? I try to not add try catch statements unless I have no other choice, and in this case there is a super simple more reasonable choice
     
    • Like Like x 1
    • Agree Agree x 1
  12. It will leave the loop because of the try/catch. It won't leave the method, as it would with a return statement. Or in other words: it will leave the method through the catch-block!
    NullPointerExceptions in general shouldn't be caught, but they shouldn't even appear!
     
    • Agree Agree x 1
  13. Code (Java):
    if(item.getType() != null && item.getType() == Material.GOLDEN_SWORD) {
    ItemStack#getType() will never be null. The NPE is caused because you call #getType() on item, which is null. Change it to this:
    Code (Java):
    if (item != null && item.getType() == Material.GOLDEN_SWORD) {
    Then remove the try/catch block and see if the error still happens.

    But yeah, try/catch blocks are for error handling. The exception still occurred, but a try/catch block expects that you do something to recover from that exception in the catch block. If you don't, then you might encounter more errors or other unexpected behavior.
     
    • Like Like x 1
    • Agree Agree x 1