Solved Right-Clicking Blocks gives NullPointerException

Discussion in 'Plugin Development' started by cosmicARTS, Aug 30, 2014.

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

    cosmicARTS

    Okay, I have a problem. In my error log it says line 257 is causing an issue... and I know it is since it is the only code I have added recently that needs some help...

    When I right click air or and item w/o a given display name in the air, I get no errors. Then, I right click a block... and boom. Errors.

    Here is my code:

    Code:java
    1. if (event.getAction() == Action.RIGHT_CLICK_BLOCK
    2. || event.getAction() == Action.RIGHT_CLICK_AIR) {
    3. if (!item.getItemMeta().hasDisplayName()) {
    4. return;
    5. }
    6. }


    It is a hub plugin... and I can't have these errors everywhere!

    How might I fix this? These items btw have their Minecraft given Display Names, not the ItemMeta I give them in my code. This error only comes when I right click w/o an item. When I use an item to click a block... nothing happens :) But when my item is null... then the errors come in :( HELP ME

    Here is my whole class:

    Show Spoiler
    Code:java
    1. package me.cosmicarts.Hub.Events;
    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.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.inventory.InventoryClickEvent;
    13. import org.bukkit.event.player.PlayerDropItemEvent;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.event.player.PlayerJoinEvent;
    16. import org.bukkit.event.player.PlayerQuitEvent;
    17. import org.bukkit.inventory.Inventory;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.inventory.meta.ItemMeta;
    20.  
    21. public class PlayerJoin implements Listener {
    22.  
    23. // Defining player list for show/hide players
    24. ArrayList<String> players = new ArrayList<String>();
    25.  
    26. // Inventories
    27. public Inventory menuinv = Bukkit.createInventory(null, 18, " "
    28. + ChatColor.DARK_GRAY + "" + ChatColor.UNDERLINE
    29. + "Server Selector");
    30. public Inventory gadgetsinv = Bukkit.createInventory(null, 9,
    31. " " + ChatColor.DARK_GRAY + "" + ChatColor.UNDERLINE
    32. + "Hub Gadgets");
    33.  
    34. // Join Items
    35. ItemStack menu = new ItemStack(Material.NETHER_STAR);
    36. {
    37.  
    38. ItemMeta menumeta = menu.getItemMeta();
    39. menumeta.setDisplayName(ChatColor.AQUA + "Menu" + ChatColor.GRAY
    40. + " - Right click to view our servers!");
    41. menu.setItemMeta(menumeta);
    42.  
    43. }
    44.  
    45. ItemStack gadgets = new ItemStack(Material.BLAZE_POWDER);
    46. {
    47.  
    48. ItemMeta gadgetsmeta = gadgets.getItemMeta();
    49. gadgetsmeta.setDisplayName(ChatColor.GOLD + "Gadgets" + ChatColor.GRAY
    50. + " - Right click to view our gadgets!");
    51. gadgets.setItemMeta(gadgetsmeta);
    52.  
    53. }
    54.  
    55. ItemStack kitpvp = new ItemStack(Material.FISHING_ROD);
    56. {
    57.  
    58. ItemMeta kitpvpmeta = kitpvp.getItemMeta();
    59. kitpvpmeta.setDisplayName(ChatColor.DARK_GRAY + "• " + ChatColor.BLUE
    60. + "KitPvP " + ChatColor.DARK_GRAY + "•");
    61. kitpvp.setItemMeta(kitpvpmeta);
    62.  
    63. }
    64.  
    65. ItemStack sg = new ItemStack(Material.FIRE);
    66. {
    67.  
    68. ItemMeta sgmeta = sg.getItemMeta();
    69. sgmeta.setDisplayName(ChatColor.DARK_GRAY + "• " + ChatColor.RED
    70. + "Survival Games " + ChatColor.DARK_GRAY + "•");
    71. sg.setItemMeta(sgmeta);
    72.  
    73. }
    74.  
    75. ItemStack oitc = new ItemStack(Material.ARROW);
    76. {
    77.  
    78. ItemMeta oitcmeta = oitc.getItemMeta();
    79. oitcmeta.setDisplayName(ChatColor.DARK_GRAY + "• "
    80. + ChatColor.LIGHT_PURPLE + "One In The Chamber "
    81. + ChatColor.DARK_GRAY + "•");
    82. oitc.setItemMeta(oitcmeta);
    83.  
    84. }
    85.  
    86. ItemStack tf2 = new ItemStack(Material.IRON_HOE);
    87. {
    88.  
    89. ItemMeta tf2meta = tf2.getItemMeta();
    90. tf2meta.setDisplayName(ChatColor.DARK_GRAY + "• " + ChatColor.GOLD
    91. + "Team Fortress 2 " + ChatColor.DARK_GRAY + "•");
    92. tf2.setItemMeta(tf2meta);
    93.  
    94. }
    95.  
    96. ItemStack factions = new ItemStack(Material.DIAMOND_PICKAXE);
    97. {
    98.  
    99. ItemMeta factionsmeta = factions.getItemMeta();
    100. factionsmeta.setDisplayName(ChatColor.DARK_GRAY + "• "
    101. + ChatColor.DARK_PURPLE + "Factions " + ChatColor.DARK_GRAY
    102. + "•");
    103. factions.setItemMeta(factionsmeta);
    104.  
    105. }
    106.  
    107. ItemStack skywars = new ItemStack(Material.SNOW_BALL);
    108. {
    109.  
    110. ItemMeta skywarsmeta = skywars.getItemMeta();
    111. skywarsmeta.setDisplayName(ChatColor.DARK_GRAY + "• " + ChatColor.AQUA
    112. + "Skywars " + ChatColor.DARK_GRAY + "•");
    113. skywars.setItemMeta(skywarsmeta);
    114.  
    115. }
    116.  
    117. ItemStack hotpotato = new ItemStack(Material.BAKED_POTATO);
    118. {
    119.  
    120. ItemMeta hotpotatometa = hotpotato.getItemMeta();
    121. hotpotatometa.setDisplayName(ChatColor.DARK_GRAY + "• "
    122. + ChatColor.DARK_RED + "Hot Potato " + ChatColor.DARK_GRAY
    123. + "•");
    124. hotpotato.setItemMeta(hotpotatometa);
    125.  
    126. }
    127.  
    128. ItemStack potatoslap = new ItemStack(Material.POISONOUS_POTATO);
    129. {
    130.  
    131. ItemMeta potatoslapmeta = potatoslap.getItemMeta();
    132. potatoslapmeta.setDisplayName(ChatColor.DARK_GRAY + "• "
    133. + ChatColor.GREEN + "Potato Slap " + ChatColor.DARK_GRAY + "•");
    134. potatoslap.setItemMeta(potatoslapmeta);
    135.  
    136. }
    137.  
    138. ItemStack uhc = new ItemStack(Material.GOLDEN_APPLE);
    139. {
    140.  
    141. ItemMeta uhcmeta = uhc.getItemMeta();
    142. uhcmeta.setDisplayName(ChatColor.DARK_GRAY + "• " + ChatColor.YELLOW
    143. + "Ultra Hardcore " + ChatColor.DARK_GRAY + "•");
    144. uhc.setItemMeta(uhcmeta);
    145.  
    146. }
    147.  
    148. ItemStack show = new ItemStack(Material.INK_SACK, 1, (short) 10);
    149. {
    150.  
    151. ItemMeta showmeta = show.getItemMeta();
    152. showmeta.setDisplayName(ChatColor.GREEN + "Players → Enabled");
    153. show.setItemMeta(showmeta);
    154.  
    155. }
    156.  
    157. ItemStack hide = new ItemStack(Material.INK_SACK, 1, (short) 8);
    158. {
    159.  
    160. ItemMeta hidemeta = hide.getItemMeta();
    161. hidemeta.setDisplayName(ChatColor.GREEN + "Players → " + ChatColor.RED
    162. + "Disabled");
    163. hide.setItemMeta(hidemeta);
    164.  
    165. }
    166.  
    167. ItemStack speed = new ItemStack(Material.FEATHER);
    168. {
    169.  
    170. ItemMeta speedmeta = speed.getItemMeta();
    171. speedmeta.setDisplayName(ChatColor.GREEN + "Add Speed Boost");
    172. speed.setItemMeta(speedmeta);
    173.  
    174. }
    175.  
    176. ItemStack jump = new ItemStack(Material.IRON_BOOTS);
    177. {
    178.  
    179. ItemMeta jumpmeta = jump.getItemMeta();
    180. jumpmeta.setDisplayName(ChatColor.GREEN + "Add Jump Boost");
    181. jump.setItemMeta(jumpmeta);
    182.  
    183. }
    184.  
    185. ItemStack tpbow = new ItemStack(Material.BOW);
    186. {
    187.  
    188. ItemMeta tpbowmeta = tpbow.getItemMeta();
    189. tpbowmeta.setDisplayName(ChatColor.GREEN + "Get TP Bow");
    190. tpbow.setItemMeta(tpbowmeta);
    191.  
    192. }
    193.  
    194. // Player Join Stuff (Give Join Items, Send MOTD, Play sounds, Give boost)
    195. @EventHandler
    196. public void onPlayerJoin(PlayerJoinEvent event) {
    197.  
    198. Player player = event.getPlayer();
    199.  
    200. if (!players.contains(player.getName())) {
    201. players.add(player.getName());
    202. }
    203.  
    204. player.getInventory().setItem(3, gadgets);
    205. player.getInventory().setItem(4, menu);
    206. player.getInventory().setItem(5, show);
    207.  
    208. menuinv.setItem(0, kitpvp);
    209. menuinv.setItem(10, sg);
    210. menuinv.setItem(2, factions);
    211. menuinv.setItem(12, oitc);
    212. menuinv.setItem(4, skywars);
    213. menuinv.setItem(14, tf2);
    214. menuinv.setItem(6, hotpotato);
    215. menuinv.setItem(16, potatoslap);
    216. menuinv.setItem(8, uhc);
    217.  
    218. gadgetsinv.setItem(0, speed);
    219. gadgetsinv.setItem(4, tpbow);
    220. gadgetsinv.setItem(8, jump);
    221.  
    222. }
    223.  
    224. // Removing players from world (removing them from lists, inventories, etc.)
    225. @EventHandler
    226. public void onQuitEvent(PlayerQuitEvent event) {
    227. Player player = event.getPlayer();
    228. if (players.contains(player.getName())) {
    229. players.remove(player.getName());
    230. }
    231. }
    232.  
    233. // Setting up commands to be run on item clicking menus and disallowing
    234. // nonops to move inv around.
    235. @EventHandler
    236. public void onInventoryClick(InventoryClickEvent event) {
    237. Player player = (Player) event.getWhoClicked();
    238. if (player.isOp()) {
    239. event.setCancelled(false);
    240. } else {
    241. event.setCancelled(true);
    242. }
    243. if (event.getWhoClicked() instanceof Player) {
    244. }
    245. }
    246.  
    247. // Setting up Show/Hide Players code.
    248. @SuppressWarnings("deprecation")
    249. @EventHandler
    250. public void onPlayerClickEvent(PlayerInteractEvent event) {
    251.  
    252. Player player = event.getPlayer();
    253. ItemStack item = player.getItemInHand();
    254.  
    255. if (event.getAction() == Action.RIGHT_CLICK_BLOCK
    256. || event.getAction() == Action.RIGHT_CLICK_AIR) {
    257. if (!item.getItemMeta().hasDisplayName()) {
    258. return;
    259. }
    260. }
    261.  
    262. if (event.getAction() == Action.RIGHT_CLICK_BLOCK
    263. || event.getAction() == Action.RIGHT_CLICK_AIR) {
    264. if (item.getItemMeta()
    265. .getDisplayName()
    266. .equalsIgnoreCase(
    267. ChatColor.GOLD + "Gadgets" + ChatColor.GRAY
    268. + " - Right click to view our gadgets!")) {
    269. player.openInventory(gadgetsinv);
    270. }
    271. }
    272.  
    273. if (event.getAction() == Action.RIGHT_CLICK_BLOCK
    274. || event.getAction() == Action.RIGHT_CLICK_AIR) {
    275. if (item.getItemMeta()
    276. .getDisplayName()
    277. .equalsIgnoreCase(
    278. ChatColor.AQUA + "Menu" + ChatColor.GRAY
    279. + " - Right click to view our servers!")) {
    280. player.openInventory(menuinv);
    281. }
    282. }
    283.  
    284. if (event.getAction() == Action.RIGHT_CLICK_BLOCK
    285. || event.getAction() == Action.RIGHT_CLICK_AIR) {
    286. if (item.getItemMeta()
    287. .getDisplayName()
    288. .equalsIgnoreCase(
    289. ChatColor.AQUA + "Gadgets" + ChatColor.GRAY
    290. + " - Right click to view our gadgets!")) {
    291. player.openInventory(gadgetsinv);
    292. }
    293. }
    294.  
    295. if (event.getAction() == Action.RIGHT_CLICK_BLOCK
    296. || event.getAction() == Action.RIGHT_CLICK_AIR) {
    297. if (item.getItemMeta().getDisplayName()
    298. .equalsIgnoreCase(ChatColor.GREEN + "Players → Enabled")) {
    299.  
    300. if (players.contains(player.getName())) {
    301. for (Player targetPlayer : Bukkit.getOnlinePlayers()) {
    302. if (player.canSee(targetPlayer)) {
    303. player.hidePlayer(targetPlayer);
    304. }
    305. players.add(player.getName());
    306. }
    307. player.getInventory().removeItem(show);
    308. player.setItemInHand(hide);
    309. player.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC
    310. + "Visibility " + ChatColor.DARK_GRAY + "→ "
    311. + ChatColor.GRAY + "Players have been toggled: "
    312. + ChatColor.RED + "Off");
    313. }
    314. } else if (item
    315. .getItemMeta()
    316. .getDisplayName()
    317. .equalsIgnoreCase(
    318. ChatColor.GREEN + "Players → " + ChatColor.RED
    319. + "Disabled")) {
    320. if (players.contains(event.getPlayer().getName())) {
    321. for (Player targetPlayer : Bukkit.getOnlinePlayers()) {
    322. if (player.canSee(targetPlayer)) {
    323. event.setCancelled(true);
    324. } else {
    325. player.showPlayer(targetPlayer);
    326. }
    327. players.remove(player.getName());
    328. }
    329. }
    330. player.getInventory().removeItem(hide);
    331. player.setItemInHand(show);
    332. player.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC
    333. + "Visibility " + ChatColor.DARK_GRAY + "→ "
    334. + ChatColor.GRAY + "Players have been toggled: "
    335. + ChatColor.GREEN + "On");
    336. }
    337. }
    338. }
    339.  
    340. @EventHandler
    341. public void noItemDrop(PlayerDropItemEvent e) {
    342. Player player = e.getPlayer();
    343. if ((e.getItemDrop().getItemStack().getType() != Material.AIR)
    344. && (!player.isOp())) {
    345. e.setCancelled(true);
    346. } else {
    347. e.setCancelled(false);
    348. }
    349. }
    350. }
    351.  
    Show Spoiler
     
  2. Offline

    Skionz

    try
    Code:
    if(!item.hasItemMeta()) {
      //
    }
     
  3. Offline

    Gater12

    cosmicARTS
    Player can hold nothing in his hands, thus item can be null.
     
    hintss likes this.
  4. Offline

    cosmicARTS

    Gater12 How would I make it null? I tried doing == null but it didn't work.
    Skionz Thanks. I'll try that but I don't know if it'll work. I'm on my phone so I'll check when I get home.
     
  5. Offline

    mine-care

    To turn a object to null use = not == but if you mean check if something is null, do if(iteminhand != null){
    Code if not null.
    }
     
  6. Offline

    cosmicARTS

  7. Offline

    Necrodoom

  8. Offline

    cosmicARTS

    Figured it out! Skionz was right xD
     
Thread Status:
Not open for further replies.

Share This Page