Solved Error when opening an inventory

Discussion in 'Spigot Plugin Development' started by ManWithBoat, May 27, 2018.

  1. Hello everyone,

    I'm trying to create a system that randomly fills a inventory with a few items. I got everything working in game but in the console there is a null point exception. I have no idea what is wrong with my code.

    My code is:
    Code (Java):
    package me.Mats.Test.Lootchests;

    import java.util.ArrayList;
    import java.util.Random;

    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;

    import me.Mats.Test.Test;
    import net.md_5.bungee.api.ChatColor;

    public class CreateLootInventory implements Listener{

        Plugin plugin = Test.getPlugin(Test.class);
       
        public void CustomInventory(Player player) {
            player.closeInventory();
            Inventory inventory = plugin.getServer().createInventory(null, 27, ChatColor.BLACK + "test");
            ArrayList<ItemStack> items = new ArrayList<ItemStack>();
       
            ItemStack item1 = new ItemStack(Material.STONE, 1);
            items.add(item1);
            ItemStack item2 = new ItemStack(Material.DIRT, 1);
            items.add(item2);
           
            Random rand = new Random();
            int amountOfItems = rand.nextInt(5) + 3;
            ArrayList<Integer> slots = new ArrayList<Integer>();
            int i = 1;
            while (i <= amountOfItems) {
                int slot = rand.nextInt(27);
                if (!slots.contains(slot)) {
                    slots.add(slot);
                    int ChosenItem = rand.nextInt(items.size());
                    inventory.setItem(slot, items.get(ChosenItem));
                    i += 1;
                }
            }
           
            player.openInventory(inventory);
           
        }
    }
     
    The error looks like:
    Code (Text):
    [code][16:57:02 ERROR]: Could not pass event InventoryOpenEvent to Test v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-9b8bba4-bd30843]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-9b8bba4-bd30843]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callInventoryOpenEvent(CraftEventFactory.java:766) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at net.minecraft.server.v1_12_R1.EntityPlayer.openContainer(EntityPlayer.java:786) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at org.bukkit.craftbukkit.v1_12_R1.entity.CraftHumanEntity.openInventory(CraftHumanEntity.java:195) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at me.Mats.Test.Lootchests.CreateLootInventory.CustomInventory(CreateLootInventory.java:44) [Test.jar:?]
            at me.Mats.Test.ChestEvents.ChestOpen.onInventoryOpen(ChestOpen.java:29) [Test.jar:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_171]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callInventoryOpenEvent(CraftEventFactory.java:766) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at net.minecraft.server.v1_12_R1.EntityPlayer.openContainer(EntityPlayer.java:786) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at net.minecraft.server.v1_12_R1.BlockChest.interact(SourceFile:342) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at net.minecraft.server.v1_12_R1.PlayerInteractManager.a(PlayerInteractManager.java:497) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:951) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:37) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_171]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_171]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot.jar:git-Spigot-9b8bba4-bd30843]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_171]
    Caused by: java.lang.NullPointerException
            at me.Mats.Test.ChestEvents.ChestOpen.onInventoryOpen(ChestOpen.java:21) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_171]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-9b8bba4-bd30843]
            ... 32 more
    [/code]

    The strange thing is that the error says there is something wrong with line 54: player.openInventory(inventory); but its working fine.
     
  2. Caused by: java.lang.NullPointerException
    at me.Mats.Test.ChestEvents.ChestOpen.onInventoryOpen(ChestOpen.java:21) ~[?:?]

    This is where your error acutally occurs. Something calls null.someMethod();
     
  3. The error would be in this code then:
    Code (Java):
    package me.Mats.Test.ChestEvents;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryOpenEvent;
    import org.bukkit.plugin.Plugin;

    import me.Mats.RPG.Lootchests.CreateLootInventory;

    public class ChestOpen implements  Listener{

        Plugin plugin = Test.getPlugin(Test.class);
       
        @EventHandler
        public void onInventoryOpen(InventoryOpenEvent event) {
           

            Player player = (Player) event.getPlayer();

            if (event.getInventory().getLocation().getBlockX() == plugin.getConfig().getInt("chest.chest1" + ".X")) {
                if (event.getInventory().getLocation().getBlockY() == plugin.getConfig().getInt("chest.chest1" + ".Y")) {
                    if (event.getInventory().getLocation().getBlockZ() == plugin.getConfig().getInt("chest.chest1" + ".Z")) {
                        event.setCancelled(true);
                        CreateLootInventory j = new CreateLootInventory();
                        j.CustomInventory(player);
                    }
                }
            }
        }
    }
     
    Line 21 is: if (event.getInventory().getLocation().getBlockX() == plugin.getConfig().getInt("chest.chest1" + ".X")) {. I don;t see a problem at that line.
     
  4. Can you poste your config.yml?
     
  5. inventory.getLocation() is probably what's null, do .getInventory().getPlayer().getLocation();
     
  6. When I do that it will get the players location and not the location of the chest

    Sure, right now it looks like:

    Code (Text):
    chest:
      chest1:
        X: -7
        Y: 26
        Z: 1241
     
  7. The Method event#getInventory()#getLocation()... doesn't exist. Use Player#getLocation()#... for getting the right position
     
  8. I fixed it, when it opened the new inventory it tried to get the location again but there is no location.