Solved Custom Inventory Error

Discussion in 'Plugin Development' started by ActivisionDevReloaded, Jul 26, 2014.

Thread Status:
Not open for further replies.
  1. Ok when you open the inventory and get a item it works perfectly but it someone is looking in the inventory and moves stuff in their own inventory it gives errors in the console. Also players can put items in the custom inventory anyone know a fix?

    Here's some of the code:

    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent e) {
    3. Player player = (Player) e.getWhoClicked();
    4. this.cfg = Core.config;
    5. if (!e.getInventory().getName().equalsIgnoreCase(this.inv.getTitle())) {
    6. return;
    7. }
    8. if (e.getCurrentItem().getItemMeta() == null) {
    9. return;
    10. }
    11. if (e.getCurrentItem().getItemMeta().getDisplayName()
    12. .contains("Diamond Helmet")) {
    13. e.setCancelled(true);
    14. if(cfg.getInt("players." + player.getName() + ".points") < 10) {
    15. player.playSound(player.getLocation(), Sound.ANVIL_LAND, 1, 1);
    16. player.sendMessage("§cYou can't afford this item!");
    17. } else {
    18. this.cfg.set("players." + player.getName() + ".points", Integer.valueOf(this.cfg.getInt("players." + player.getName() + ".points") - 10));
    19. ItemStack helm = new ItemStack(Material.DIAMOND_HELMET);
    20. Enchantment enchantment = new EnchantmentWrapper(0);
    21. helm.addEnchantment(enchantment, 2);
    22. player.getInventory().addItem(helm);
    23. player.playSound(player.getLocation(), Sound.NOTE_PLING,
    24. 1.0F, 1.0F);
    25. player.sendMessage("§aYou have just purchased a Diamond Helmet!");
    26. Core.savePointsConfig();
    27. }
    28. }
    29. }

    Also heres the errors in the console:
    [01:53:23 ERROR]: Could not pass event InventoryClickEvent to ProdigyShops v1.0 org.bukkit.event.EventException                                                        at$1.execute( ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                  at org.bukkit.plugin.RegisteredListener.callEvent( ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                    at org.bukkit.plugin.SimplePluginManager.fireEvent( [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                  at org.bukkit.plugin.SimplePluginManager.callEvent( [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                  at net.minecraft.server.v1_7_R3.PlayerConnection.a( [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                    at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.a(SourceFile:32) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                          at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.handle(SourceFile:10) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                    at net.minecraft.server.v1_7_R3.NetworkManager.a( [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                                at net.minecraft.server.v1_7_R3.MinecraftServer.v( [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                      at net.minecraft.server.v1_7_R3.DedicatedServer.v( [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                      at net.minecraft.server.v1_7_R3.MinecraftServer.u( [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                      at [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                    at [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]              Caused by: java.lang.NullPointerException                                              at com.nickyc27.prodigyshops.shops.StarterShop.onInventoryClick( ~[?:?]                                                                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_65]                                                                                    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_65]                                                                                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_65]                                                                                at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_65]                at$1.execute( ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]                  ... 13 more                                                            >                                                                              
  2. Offline


  3. Offline


  4. Offline


    ActivisionDevReloaded cfg can be null, getDisplayName can be null, why the useless Integer wrapping, why the EnchantmentWrapper
  5. fireblast709

    The cfg is not null, I initialized it on line 4. It says getDisplayName.contains("Diamond Helmet"), And the enchantment things is for the player to get the item they bought with the enchantment... I don't see any of what you said useful. Not to be mean but your answers are all in the code. I'm asking for a fix for the errors and im pretty sure those things aren't thing problem since they should be all initialized.

    That is the entire stacktrace .

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 9, 2016
  6. Offline


  7. Niknea
    Move the little gray box on the botttom to see more.
  8. Offline


    ActivisionDevReloaded you assign Core.config to it, which can be null at that point. As I said before, getDisplayName() can be null, thus calling contains() will throw a NullPointerException if so. You should -not- use the EnchantmentWrapper class, use the constants found in Enchantment. Lastly, that is not the full stacktrace. Check your logs, you will find the full stacktrace there. Especially the 'Caused by...', which is likely to be present, will tell you a lot about the error in combination with the lines below it.

    The only things that do not help you solve the issues would be the remarks about the Integer wrapping and the Enchantments (though they are things you should change)
  9. Offline


  10. Offline


    What is line 206?
    Caused by: java.lang.NullPointerException
    at com.nickyc27.prodigyshops.shops.StarterShop.onInventoryClick( ~[?:?]

  11. Here is your pastebin with the stacktrace:

    Integer wrapping isn't the issue since its been happening before that. The integer wrapping works fine so leave it at that. Core.config is initialized in the class and the main class (if you need the Core class just ask). Explain more on the getDisplayName().

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 9, 2016
  12. Offline


    ActivisionDevReloaded getDisplayName() returns null if the ItemStack doesn't have a display name (which is set by a call to ItemMeta#setDisplayName(String) followed by a ItemStack#setItemMeta(ItemMeta)).

    The Integer wrap is just redundant (since you already have an int) and looks a bit messy imo.

    xTigerRebornx didn't notice the small grey scrolling bar xD (I'm on my phone atm)
  13. fireblast709

    I'm pretty sure the error is coming from the getDisplayName() do you have a fix?
  14. Offline


Thread Status:
Not open for further replies.

Share This Page