Hello Everyone, I am getting an NPE and I don't know why. Error: Code: [17:30:40] [Server thread/ERROR]: Could not pass event InventoryClickEvent to Quidditch v1.0 org.bukkit.event.EventException at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:486) [Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:471) [Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:1361) [Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.a(SourceFile:32) [Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.handle(SourceFile:10) [Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] Caused by: java.lang.NullPointerException at me.pieman.quidditch.Quidditch.onInventoryClick(Quidditch.java:91) ~[?:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_45] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_45] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_45] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_45] at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:318) ~[Craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks] ... 13 more [17:35:33] InventoryClickEvent: Code:java @EventHandler public void onInventoryClick(InventoryClickEvent e){ Player p = (Player) e.getWhoClicked(); p.sendMessage("Working!!!"); if(!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return; p.sendMessage("Working"); if(e.getCurrentItem() == null) return; p.sendMessage("Still Working"); if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("gryffindor")){ e.setCancelled(true); Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Gryffindor!"); p.closeInventory(); p.sendMessage("Should Be Working"); } Whole Class: Code:java package me.pieman.quidditch; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.Material;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.inventory.InventoryClickEvent;import org.bukkit.inventory.Inventory;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.meta.ItemMeta;import org.bukkit.plugin.java.JavaPlugin; public class Quidditch extends JavaPlugin implements Listener{ private Inventory inv; public void onEnable(){ Bukkit.getServer().getPluginManager().registerEvents(this, this); } @SuppressWarnings("deprecation") public void open(Player p){ inv = Bukkit.createInventory(null, 9, "Teams"); ItemStack red = new ItemStack(Material.WOOL, 1, (byte) 14); ItemMeta redmeta = red.getItemMeta(); redmeta.setDisplayName(ChatColor.RED+"Gryffindor"); red.setItemMeta(redmeta); ItemStack yellow = new ItemStack(Material.WOOL, 1, (byte) 4); ItemMeta yellowmeta = yellow.getItemMeta(); yellowmeta.setDisplayName(ChatColor.YELLOW+"Hufflepuff"); yellow.setItemMeta(yellowmeta); ItemStack blue = new ItemStack(Material.WOOL, 1, (byte) 11); ItemMeta bluemeta = blue.getItemMeta(); bluemeta.setDisplayName(ChatColor.DARK_BLUE+"Ravenclaw"); blue.setItemMeta(bluemeta); ItemStack green = new ItemStack(Material.WOOL, 1, (byte) 13); ItemMeta greenmeta = green.getItemMeta(); greenmeta.setDisplayName(ChatColor.GREEN+"Slytherin"); green.setItemMeta(greenmeta); inv.clear(); inv.setItem(3, new ItemStack(red)); inv.setItem(4, new ItemStack(yellow)); inv.setItem(5, new ItemStack(blue)); inv.setItem(6, new ItemStack(green)); p.openInventory(inv); p.updateInventory(); } public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){ if(label.equalsIgnoreCase("quidditch")){ if(args.length == 0){ sender.sendMessage(ChatColor.RED + "Wrong usage /quidditch join"); return true; } if(!(sender instanceof Player)){ sender.sendMessage(ChatColor.RED + "Only players can use the command!"); return true; } Player p = (Player) sender; if(args[0].equalsIgnoreCase("join")){ if(p.hasPermission("quidditch.play")){ open(p); return true; } else { p.sendMessage(ChatColor.RED + "Sorry you do not have permission!"); return true; } } } return true; } @EventHandler public void onInventoryClick(InventoryClickEvent e){ Player p = (Player) e.getWhoClicked(); p.sendMessage("Working!!!"); if(!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return; p.sendMessage("Working"); if(e.getCurrentItem() == null) return; p.sendMessage("Still Working"); if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("gryffindor")){ e.setCancelled(true); Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Gryffindor!"); p.closeInventory(); p.sendMessage("Should Be Working"); } if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("hufflepuff")){ e.setCancelled(true); Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Hufflepuff!"); p.closeInventory(); } if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("ravenclaw")){ e.setCancelled(true); Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Ravenclaw!"); p.closeInventory(); } if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("slytherin")){ e.setCancelled(true); Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Slytherin!"); p.closeInventory(); } } } Line 91: Code:java if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("gryffindor")){
SuperOmegaCow I am testing that. The debug lines go to "Still Working". It works through "Working!!!" "Working" but it stops at "Still Working".
If the item has no ItemMeta it will return null, hence your NPE. Check first: Code:java if (ItemStack.hasItemMeta()) { // can use .getItemMeta()} else { // is null}
1Rogue alex123099 SuperOmegaCow I assign a name to it though so how would it be null? Code:java ItemStack red = new ItemStack(Material.WOOL, 1, (byte) 14); ItemMeta redmeta = red.getItemMeta(); redmeta.setDisplayName(ChatColor.RED+"Gryffindor"); red.setItemMeta(redmeta); 1Rogue It does return null so how would I fix this? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
SuperOmegaCow Alshain01 Well obviously checking the item meta is not working for me so is there a way to check if the item they're clicking on is red wool? is there a way to check if the item they're clicking on is red wool? EDIT by Moderator: merged posts, please use the edit button instead of double posting.