[SOLVED] NullPointerException getItem Inventory

Discussion in 'Spigot Plugin Development' started by XelfeGamer, Jul 8, 2015.

  1. Hey guys i have a new problem and i can't fix it, if somebody can help me would be great, my code:

    Code (Text):

    @EventHandler
        public void onPlayerInventoryOpen(InventoryOpenEvent event) {
            if(event.getInventory().getHolder() instanceof Chest || event.getInventory().getHolder() instanceof DoubleChest) {
                Chest chest = (Chest)event.getInventory().getHolder();
                Location loc = chest.getLocation();
                    if(!cofres.contains(loc)) {
                        cofres.add(loc);
                        Integer contador = 0;
                        while(true) {
                            for(String s : main.getConfig().getStringList("items")) {
                                String[] args = s.split(" ");
                                Integer pro = Integer.parseInt(args[0]);
                                Integer random = new Random().nextInt(100);
                                if(random + 1 <= pro) {
                                    ItemStack item = new ItemStack(Material.matchMaterial(args[1]), Integer.parseInt(args[2]));
                                    if(chest.getInventory().contains(item)) continue;
                                    if(item != null) {
                                            Integer slot = new Random().nextInt(53);
                                            Inventory inv = chest.getInventory();
                                            if(slot > inv.getSize()) slot = inv.getSize();
                                            boolean flag = false;
                                            if(slot == null) {
                                                Bukkit.getConsoleSender().sendMessage("┬ža┬žlNULLO");
                                                return;
                                            }
                                            if(inv.getContents().length == 0) {
                                                inv.setItem(slot, item);
                                            } else
                                            if(inv.getItem(slot).getType() == null) {
                                                inv.setItem(slot, item);
                                                flag = true;
                                            } else
                                            if(!flag) {
                                                if(inv.getItem(slot).getType() == Material.AIR) {
                                                    inv.setItem(slot, item);
                                                }
                                            }
                                           
                                            contador++;
                                    }
                                   
                                }
                            }
                            if(contador > 6) break;
                        }
                    }
            }
        }
     
    The error:
    Code (Text):
    [19:05:43 ERROR]: Could not pass event InventoryOpenEvent to SkyWars v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:302) ~[spigot.jar:git-Bukkit-e91aed8]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Bukkit-e91aed8]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [spigot.jar:git-Bukkit-e91aed8]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [spigot.jar:git-Bukkit-e91aed8]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callInventoryO
    penEvent(CraftEventFactory.java:698) [spigot.jar:git-Bukkit-e91aed8]
            at net.minecraft.server.v1_8_R3.EntityPlayer.openContainer(EntityPlayer.
    java:648) [spigot.jar:git-Bukkit-e91aed8]
            at net.minecraft.server.v1_8_R3.BlockChest.interact(SourceFile:339) [spi
    got.jar:git-Bukkit-e91aed8]
            at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInt
    eractManager.java:495) [spigot.jar:git-Bukkit-e91aed8]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :705) [spigot.jar:git-Bukkit-e91aed8]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:52) [spigot.jar:git-Bukkit-e91aed8]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:1) [spigot.jar:git-Bukkit-e91aed8]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot.jar:git-Bukkit-e91aed8]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.7.0_79]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_79]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spig
    ot.jar:git-Bukkit-e91aed8]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:6
    76) [spigot.jar:git-Bukkit-e91aed8]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    35) [spigot.jar:git-Bukkit-e91aed8]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    32) [spigot.jar:git-Bukkit-e91aed8]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :540) [spigot.jar:git-Bukkit-e91aed8]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.NullPointerException
            at events.RellenarCofres.onPlayerInventoryOpen(RellenarCofres.java:61) ~
    [?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _79]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _79]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_79]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_79]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:300) ~[spigot.jar:git-Bukkit-e91aed8]
            ... 19 more
    Line 61:
    Code (Text):
    if(inv.getItem(slot).getType() == null) {
    Thanks in advance.
     
  2. inv.getItem(slot) is null.
     
  3. I checked this if(inv.getItem(slot) == null) { and my server crash, i get this message:

    [19:36:58 FATAL]: A single server tick took 60,00 seconds (should be max 0,05)
    [19:36:58 FATAL]: Considering it to be crashed, server will forcibly shutdown.
     
  4. Well, probably because you are doing this work inside a while loop. One that's always true... Thats an infinite loop, my friend.
     
    • Agree Agree x 1
    • Winner Winner x 1
  5. You are right, thanks you, now my plugin works great!
     
  6. Change
    Code (Text):

    if(inv.getItem(slot).getType() == null) {
     
    to
    Code (Text):

    if(inv.getItem(slot) == null) {
     
    This is because you are looking for the item type, not the item. The item is null, but you're checking if the type of the item is null, which will not work. Checking if the item itself is null, however, will.
     
  7. Mark as solved please
     
  8. How?
     
  9. a the top, thread tools, change title and add a [SOVLED] before the title, but do NOT delete the title
     
  10. Done :)