NPE

Discussion in 'Plugin Development' started by PieMan456, Feb 24, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    PieMan456

    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
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent e){
    3. Player p = (Player) e.getWhoClicked();
    4. p.sendMessage("Working!!!");
    5. if(!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
    6. p.sendMessage("Working");
    7. if(e.getCurrentItem() == null) return;
    8. p.sendMessage("Still Working");
    9. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("gryffindor")){
    10. e.setCancelled(true);
    11. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Gryffindor!");
    12. p.closeInventory();
    13. p.sendMessage("Should Be Working");
    14. }

    Whole Class:
    Code:java
    1. package me.pieman.quidditch;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.inventory.InventoryClickEvent;
    12. import org.bukkit.inventory.Inventory;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Quidditch extends JavaPlugin implements Listener{
    18.  
    19. private Inventory inv;
    20.  
    21. public void onEnable(){
    22. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    23. }
    24.  
    25. @SuppressWarnings("deprecation")
    26. public void open(Player p){
    27. inv = Bukkit.createInventory(null, 9, "Teams");
    28.  
    29. ItemStack red = new ItemStack(Material.WOOL, 1, (byte) 14);
    30. ItemMeta redmeta = red.getItemMeta();
    31. redmeta.setDisplayName(ChatColor.RED+"Gryffindor");
    32. red.setItemMeta(redmeta);
    33.  
    34. ItemStack yellow = new ItemStack(Material.WOOL, 1, (byte) 4);
    35. ItemMeta yellowmeta = yellow.getItemMeta();
    36. yellowmeta.setDisplayName(ChatColor.YELLOW+"Hufflepuff");
    37. yellow.setItemMeta(yellowmeta);
    38.  
    39. ItemStack blue = new ItemStack(Material.WOOL, 1, (byte) 11);
    40. ItemMeta bluemeta = blue.getItemMeta();
    41. bluemeta.setDisplayName(ChatColor.DARK_BLUE+"Ravenclaw");
    42. blue.setItemMeta(bluemeta);
    43.  
    44. ItemStack green = new ItemStack(Material.WOOL, 1, (byte) 13);
    45. ItemMeta greenmeta = green.getItemMeta();
    46. greenmeta.setDisplayName(ChatColor.GREEN+"Slytherin");
    47. green.setItemMeta(greenmeta);
    48.  
    49. inv.clear();
    50. inv.setItem(3, new ItemStack(red));
    51. inv.setItem(4, new ItemStack(yellow));
    52. inv.setItem(5, new ItemStack(blue));
    53. inv.setItem(6, new ItemStack(green));
    54.  
    55. p.openInventory(inv);
    56. p.updateInventory();
    57. }
    58.  
    59. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    60. if(label.equalsIgnoreCase("quidditch")){
    61. if(args.length == 0){
    62. sender.sendMessage(ChatColor.RED + "Wrong usage /quidditch join");
    63. return true;
    64. }
    65. if(!(sender instanceof Player)){
    66. sender.sendMessage(ChatColor.RED + "Only players can use the command!");
    67. return true;
    68. }
    69. Player p = (Player) sender;
    70. if(args[0].equalsIgnoreCase("join")){
    71. if(p.hasPermission("quidditch.play")){
    72. open(p);
    73. return true;
    74. } else {
    75. p.sendMessage(ChatColor.RED + "Sorry you do not have permission!");
    76. return true;
    77. }
    78. }
    79. }
    80. return true;
    81. }
    82.  
    83. @EventHandler
    84. public void onInventoryClick(InventoryClickEvent e){
    85. Player p = (Player) e.getWhoClicked();
    86. p.sendMessage("Working!!!");
    87. if(!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
    88. p.sendMessage("Working");
    89. if(e.getCurrentItem() == null) return;
    90. p.sendMessage("Still Working");
    91. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("gryffindor")){
    92. e.setCancelled(true);
    93. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Gryffindor!");
    94. p.closeInventory();
    95. p.sendMessage("Should Be Working");
    96. }
    97. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("hufflepuff")){
    98. e.setCancelled(true);
    99. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Hufflepuff!");
    100. p.closeInventory();
    101. }
    102. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("ravenclaw")){
    103. e.setCancelled(true);
    104. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Ravenclaw!");
    105. p.closeInventory();
    106. }
    107. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("slytherin")){
    108. e.setCancelled(true);
    109. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Slytherin!");
    110. p.closeInventory();
    111. }
    112. }
    113.  
    114. }
    115.  

    Line 91:
    Code:java
    1. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("gryffindor")){
     
  2. Offline

    SuperOmegaCow

    PieMan456
    Check if getCurrentItem returns null. It could also be getItemMeta
     
  3. Offline

    PieMan456

    SuperOmegaCow
    I am testing that. The debug lines go to "Still Working". It works through "Working!!!" "Working" but it stops at "Still Working".
     
  4. Offline

    SuperOmegaCow

    PieMan456 does it get to Should be working?
     
  5. Offline

    alex123099

    PieMan456
    It might be that the item's display name returns null o.o
     
  6. Offline

    1Rogue

    If the item has no ItemMeta it will return null, hence your NPE. Check first:

    Code:java
    1. if (ItemStack.hasItemMeta()) {
    2. // can use .getItemMeta()
    3. } else {
    4. // is null
    5. }
     
  7. Offline

    PieMan456

    1Rogue alex123099 SuperOmegaCow
    I assign a name to it though so how would it be null?
    Code:java
    1. ItemStack red = new ItemStack(Material.WOOL, 1, (byte) 14);
    2. ItemMeta redmeta = red.getItemMeta();
    3. redmeta.setDisplayName(ChatColor.RED+"Gryffindor");
    4. 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.
     
    Last edited by a moderator: Jun 6, 2016
  8. Offline

    Alshain01

    He showed you in his post, check hasItemMeta first.
     
  9. Offline

    SuperOmegaCow

    PieMan456 you only assign this data when the player actually types the command.
     
  10. Offline

    PieMan456

    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.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page