NullPointerException

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

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

    PieMan456

    Hello Everyone,

    I am getting a nullpointerexception and I don't know why. Thanks for any help!
    Error:
    Code:
    [21:14:01] [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:89) ~[?:?]
        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
    
    Plugin:
    Code:java
    1. package me.pieman.quidditch;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.inventory.InventoryClickEvent;
    14. import org.bukkit.inventory.Inventory;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class Quidditch extends JavaPlugin implements Listener{
    20.  
    21. public void onEnable(){
    22. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    23. }
    24.  
    25. @SuppressWarnings("deprecation")
    26. public void open(Player p){
    27. Inventory inv = Bukkit.createInventory(null, 9, "Teams");
    28.  
    29. ItemStack red = new ItemStack(Material.WOOL, (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, (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, (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, (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. ItemStack clicked = e.getCurrentItem();
    87. Inventory inv = e.getInventory();
    88. if(inv.getName().equalsIgnoreCase("Teams")){
    89. if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("gryffindor")){
    90. e.setCancelled(true);
    91. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Gryffindor!");
    92. p.closeInventory();
    93. return;
    94. }
    95. if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("hufflepuff")){
    96. e.setCancelled(true);
    97. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Hufflepuff!");
    98. p.closeInventory();
    99. return;
    100. }
    101. if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("ravenclaw")){
    102. e.setCancelled(true);
    103. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Ravenclaw!");
    104. p.closeInventory();
    105. return;
    106. }
    107. if(clicked.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. return;
    112. } else {
    113. return;
    114. }
    115. }
    116. return;
    117. }
    118.  
    119. }
    120.  

    Line 89:
    Code:java
    1. if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("gryffindor")){
     
  2. Offline

    Codex Arcanum

    In a nutshell, you're going to want to add some debug code to check which objects used on line 89 are null. So, you should check in order whether clicked == null, clicked.getItemMeta() == null, clicked.getItemMeta().getDisplayName() == null. If any of those conditions are true, have your plugin print something out to the console that tells you which one it was, and it'll have identified your problem.
     
  3. Offline

    jesus997

    Check if the item is named. Example:

    Code:java
    1.  
    2. if(item.getDisplayName() == null){
    3. return;
    4. }


    This will prevent an error out when the item is not called "gryffindor".

    Sorry for my bad english.
     
  4. Offline

    PieMan456

  5. Offline

    Codex Arcanum

    You have what? If ItemMeta is null, then that answers your question. You are getting an NPE because you are running .getDisplayName() on an object that is null.
     
  6. Offline

    PieMan456

    Yeah but I have this 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);


    Anyone know why this is not working?
    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. public void onEnable(){
    20. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    21. }
    22.  
    23. @SuppressWarnings("deprecation")
    24. public void open(Player p){
    25. Inventory inv = Bukkit.createInventory(null, 9, "Teams");
    26.  
    27. ItemStack red = new ItemStack(Material.WOOL, 1, (byte) 14);
    28. ItemMeta redmeta = red.getItemMeta();
    29. redmeta.setDisplayName(ChatColor.RED+"Gryffindor");
    30. red.setItemMeta(redmeta);
    31.  
    32. ItemStack yellow = new ItemStack(Material.WOOL, 1, (byte) 4);
    33. ItemMeta yellowmeta = yellow.getItemMeta();
    34. yellowmeta.setDisplayName(ChatColor.YELLOW+"Hufflepuff");
    35. yellow.setItemMeta(yellowmeta);
    36.  
    37. ItemStack blue = new ItemStack(Material.WOOL, 1, (byte) 11);
    38. ItemMeta bluemeta = blue.getItemMeta();
    39. bluemeta.setDisplayName(ChatColor.DARK_BLUE+"Ravenclaw");
    40. blue.setItemMeta(bluemeta);
    41.  
    42. ItemStack green = new ItemStack(Material.WOOL, 1, (byte) 13);
    43. ItemMeta greenmeta = green.getItemMeta();
    44. greenmeta.setDisplayName(ChatColor.GREEN+"Slytherin");
    45. green.setItemMeta(greenmeta);
    46.  
    47. inv.clear();
    48. inv.setItem(3, new ItemStack(red));
    49. inv.setItem(4, new ItemStack(yellow));
    50. inv.setItem(5, new ItemStack(blue));
    51. inv.setItem(6, new ItemStack(green));
    52.  
    53. p.openInventory(inv);
    54. p.updateInventory();
    55. }
    56.  
    57. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    58. if(label.equalsIgnoreCase("quidditch")){
    59. if(args.length == 0){
    60. sender.sendMessage(ChatColor.RED + "Wrong usage /quidditch join");
    61. return true;
    62. }
    63. if(!(sender instanceof Player)){
    64. sender.sendMessage(ChatColor.RED + "Only players can use the command!");
    65. return true;
    66. }
    67. Player p = (Player) sender;
    68. if(args[0].equalsIgnoreCase("join")){
    69. if(p.hasPermission("quidditch.play")){
    70. open(p);
    71. return true;
    72. } else {
    73. p.sendMessage(ChatColor.RED + "Sorry you do not have permission!");
    74. return true;
    75. }
    76. }
    77. }
    78. return true;
    79. }
    80.  
    81. @EventHandler
    82. public void onInventoryClick(InventoryClickEvent e){
    83. Player p = (Player) e.getWhoClicked();
    84. ItemStack clicked = e.getCurrentItem();
    85. Inventory inv = e.getInventory();
    86. if(inv.getName().equalsIgnoreCase("Teams")){
    87. /*if(clicked == null){
    88.   p.sendMessage("Yeah");
    89.   return;
    90.   }
    91.   if(clicked.getItemMeta() == null){
    92.   e.setCancelled(true);
    93.   p.sendMessage("Yeah1");
    94.   return;
    95.   }
    96.   if(clicked.getItemMeta().getDisplayName() == null){
    97.   p.sendMessage("Yeah2");
    98.   return;
    99.   }*/
    100. if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("gryffindor")){
    101. e.setCancelled(true);
    102. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Gryffindor!");
    103. p.closeInventory();
    104. return;
    105. }
    106. if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("hufflepuff")){
    107. e.setCancelled(true);
    108. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Hufflepuff!");
    109. p.closeInventory();
    110. return;
    111. }
    112. if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("ravenclaw")){
    113. e.setCancelled(true);
    114. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Ravenclaw!");
    115. p.closeInventory();
    116. return;
    117. }
    118. if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase("slytherin")){
    119. e.setCancelled(true);
    120. Bukkit.getServer().broadcastMessage(ChatColor.DARK_AQUA+"[" + ChatColor.AQUA + "AG Quidditch" + ChatColor.DARK_AQUA + "] " + p.getName() + " has joined Slytherin!");
    121. p.closeInventory();
    122. return;
    123. } else {
    124. return;
    125. }
    126. }
    127. return;
    128. }
    129.  
    130. }
    131.  


    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