Solved NullPointerException

Discussion in 'Plugin Development' started by PieMan456, Dec 2, 2013.

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

    PieMan456

    Hello Everyone,

    I am getting a nullpointerexception nad I do not know why. Here is the error:
    Code:
    2013-12-02 16:49:00 [SEVERE] Could not pass event PlayerInteractEvent to Eggsplosions v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:190)
        at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:160)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:610)
        at net.minecraft.server.v1_6_R3.Packet15Place.handle(SourceFile:58)
        at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
        at me.pieman.eggs.Eggs.onPlayerInteract(Eggs.java:276)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 16 more
    Here is line 276:
    Code:java
    1. if(bomb.contains(b.getLocation())){


    Here is whole class:
    Code:java
    1. package me.pieman.eggs;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import net.milkbowl.vault.economy.Economy;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Location;
    10. import org.bukkit.Material;
    11. import org.bukkit.block.Block;
    12. import org.bukkit.block.Sign;
    13. import org.bukkit.entity.Egg;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.entity.Projectile;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.event.block.Action;
    19. import org.bukkit.event.entity.ProjectileHitEvent;
    20. import org.bukkit.event.inventory.InventoryClickEvent;
    21. import org.bukkit.event.player.PlayerEggThrowEvent;
    22. import org.bukkit.event.player.PlayerInteractEvent;
    23. import org.bukkit.event.player.PlayerMoveEvent;
    24. import org.bukkit.inventory.Inventory;
    25. import org.bukkit.inventory.ItemStack;
    26. import org.bukkit.inventory.meta.ItemMeta;
    27. import org.bukkit.metadata.FixedMetadataValue;
    28. import org.bukkit.plugin.RegisteredServiceProvider;
    29. import org.bukkit.plugin.java.JavaPlugin;
    30. import org.bukkit.util.Vector;
    31.  
    32. public class Eggs extends JavaPlugin implements Listener {
    33.  
    34. public Eggs plugin;
    35.  
    36. public ArrayList<String> player = new ArrayList<String>();
    37.  
    38. public ArrayList<Location> mine = new ArrayList<Location>();
    39.  
    40. public ArrayList<Location> bomb = new ArrayList<Location>();
    41.  
    42. public static Economy econ = null;
    43.  
    44. public void onEnable(){
    45. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    46. if (!setupEconomy() ) {
    47. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    48. getServer().getPluginManager().disablePlugin(this);
    49. return;
    50. }
    51. }
    52.  
    53. private boolean setupEconomy() {
    54. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    55. return false;
    56. }
    57. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    58. if (rsp == null) {
    59. return false;
    60. }
    61. econ = rsp.getProvider();
    62. return econ != null;
    63. }
    64.  
    65. @EventHandler
    66. public void onProjectileHit(ProjectileHitEvent e){
    67. Projectile p = e.getEntity();
    68. if(!(p instanceof Egg)) return;
    69. Egg egg = (Egg) p;
    70. if(egg.hasMetadata("grenade")){
    71. double eggx = egg.getLocation().getX();
    72. double eggy = egg.getLocation().getY();
    73. double eggz = egg.getLocation().getZ();
    74. egg.getWorld().createExplosion(eggx, eggy, eggz, 4F, false, false);
    75. return;
    76. }
    77. if(egg.hasMetadata("landmine")){
    78. Block b = egg.getLocation().getBlock();
    79. mine.add(b.getLocation());
    80. return;
    81. }
    82. if(egg.hasMetadata("bomb")){
    83. final Player shooter = (Player) egg.getShooter();
    84. final Block b = egg.getLocation().getBlock();
    85. bomb.add(b.getLocation());
    86. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    87. public void run(){
    88. if(player.contains(shooter.getName())){
    89. b.getWorld().createExplosion(b.getX(), b.getY(), b.getZ(), 4F, true, true);
    90. bomb.remove(b.getLocation());
    91. Bukkit.getServer().getScheduler().cancelTasks(plugin);
    92. return;
    93. }
    94. shooter.sendMessage(ChatColor.GREEN + "Bomb is going off in 10 seconds!");
    95. player.add(shooter.getName());
    96. }
    97. }, 20 * 10, 20 * 10);
    98. }
    99. }
    100.  
    101. @EventHandler
    102. public void onPlayerMove(PlayerMoveEvent e){
    103. Player p = e.getPlayer();
    104. ArrayList<Location> mine = this.mine;
    105. for(Location mloc : mine){
    106. Location ploc = p.getLocation();
    107. if(ploc.getX() == mloc.getX() && ploc.getY() == mloc.getY() && ploc.getX() == mloc.getX()){
    108. mloc.getWorld().createExplosion(ploc.getX(), ploc.getY(), ploc.getZ(), 4F, false, false);
    109. mine.remove(mloc);
    110. }
    111. }
    112. }
    113.  
    114. @SuppressWarnings("deprecation")
    115. public void openInventory(Player p){
    116. Inventory inv = Bukkit.createInventory(null, 9, "Bomb Factory");
    117.  
    118. ItemStack grenade = new ItemStack(Material.EGG);
    119. ItemMeta gmeta = grenade.getItemMeta();
    120. ArrayList<String> g = new ArrayList<String>();
    121. g.add(ChatColor.DARK_AQUA + "Cost: " + getConfig().getDouble("grenadeprice"));
    122. gmeta.setLore(g);
    123. gmeta.setDisplayName(ChatColor.GREEN + "Grenade");
    124. grenade.setItemMeta(gmeta);
    125.  
    126. ItemStack bomb = new ItemStack(Material.EGG);
    127. ItemMeta bmeta = bomb.getItemMeta();
    128. ArrayList<String> b = new ArrayList<String>();
    129. b.add(ChatColor.DARK_AQUA + "Cost: " + getConfig().getDouble("bombprice"));
    130. bmeta.setLore(b);
    131. bmeta.setDisplayName(ChatColor.GREEN + "Bomb");
    132. bomb.setItemMeta(bmeta);
    133.  
    134. ItemStack landmine = new ItemStack(Material.EGG);
    135. ItemMeta lmeta = landmine.getItemMeta();
    136. ArrayList<String> l = new ArrayList<String>();
    137. l.add(ChatColor.DARK_AQUA + "Cost: " + getConfig().getDouble("landmineprice"));
    138. lmeta.setLore(l);
    139. lmeta.setDisplayName(ChatColor.GREEN + "LandMine");
    140. landmine.setItemMeta(lmeta);
    141.  
    142. ItemStack wirecutters = new ItemStack(Material.FLINT_AND_STEEL);
    143. ItemMeta wmeta = wirecutters.getItemMeta();
    144. ArrayList<String> w = new ArrayList<String>();
    145. w.add(ChatColor.DARK_AQUA + "Cost: " + getConfig().getDouble("wirecuttersprice"));
    146. wmeta.setLore(w);
    147. wmeta.setDisplayName(ChatColor.GREEN + "WireCutters");
    148. wirecutters.setItemMeta(wmeta);
    149.  
    150. ItemStack money = new ItemStack(Material.DIAMOND);
    151. ItemMeta mmeta = money.getItemMeta();
    152. mmeta.setDisplayName(ChatColor.DARK_PURPLE + "Balance: " + econ.getBalance(p.getName()));
    153. money.setItemMeta(mmeta);
    154.  
    155. p.openInventory(inv);
    156.  
    157. inv.clear();
    158. inv.setItem(0, new ItemStack(grenade));
    159. inv.setItem(2, new ItemStack(bomb));
    160. inv.setItem(4, new ItemStack(money));
    161. inv.setItem(6, new ItemStack(landmine));
    162. inv.setItem(8, new ItemStack(wirecutters));
    163.  
    164. p.updateInventory();
    165. }
    166.  
    167. @EventHandler
    168. public void onEggThrow(PlayerEggThrowEvent e){
    169. if(e.getEgg().hasMetadata("grenade")){
    170. e.setHatching(true);
    171. }
    172. if(e.getEgg().hasMetadata("landmine")){
    173. e.setHatching(true);
    174. }
    175. if(e.getEgg().hasMetadata("bomb")){
    176. e.setHatching(true);
    177. }
    178. }
    179.  
    180. @EventHandler
    181. public void onInventoryClick(InventoryClickEvent e){
    182. if(!e.getInventory().getName().equalsIgnoreCase("Bomb Factory")) return;
    183. if(!(e.getWhoClicked() instanceof Player)) return;
    184. Player p = (Player) e.getWhoClicked();
    185. if (p.getInventory().firstEmpty() == -1) {
    186. p.sendMessage(ChatColor.RED + "Your inventory is full!");
    187. return;
    188. }
    189. if(e.getCurrentItem().getType() == Material.DIAMOND){
    190. e.setCancelled(true);
    191. }
    192. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Grenade")){
    193. e.setCancelled(true);
    194. if(econ.getBalance(p.getName()) < getConfig().getDouble("grenadeprice")){
    195. p.sendMessage(ChatColor.RED + "You do not have enough money!");
    196. return;
    197. } else {
    198. ItemStack grenade = new ItemStack(Material.EGG);
    199. ItemMeta gmeta = grenade.getItemMeta();
    200. gmeta.setDisplayName(ChatColor.GREEN + "Grenade");
    201. grenade.setItemMeta(gmeta);
    202. p.getInventory().addItem(new ItemStack(grenade));
    203. econ.withdrawPlayer(p.getName(), getConfig().getDouble("grenadeprice"));
    204. }
    205. }
    206. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Bomb")){
    207. e.setCancelled(true);
    208. if(econ.getBalance(p.getName()) < getConfig().getDouble("bombprice")){
    209. p.sendMessage(ChatColor.RED + "You do not have enough money!");
    210. return;
    211. } else {
    212. ItemStack bomb = new ItemStack(Material.EGG);
    213. ItemMeta bmeta = bomb.getItemMeta();
    214. bmeta.setDisplayName(ChatColor.GREEN + "Bomb");
    215. bomb.setItemMeta(bmeta);
    216. p.getInventory().addItem(new ItemStack(bomb));
    217. econ.withdrawPlayer(p.getName(), getConfig().getDouble("bombprice"));
    218. }
    219. }
    220. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "LandMine")){
    221. e.setCancelled(true);
    222. if(econ.getBalance(p.getName()) < getConfig().getDouble("landmineprice")){
    223. p.sendMessage(ChatColor.RED + "You do not have enough money!");
    224. return;
    225. } else {
    226. ItemStack landmine = new ItemStack(Material.EGG);
    227. ItemMeta lmeta = landmine.getItemMeta();
    228. lmeta.setDisplayName(ChatColor.GREEN + "LandMine");
    229. landmine.setItemMeta(lmeta);
    230. p.getInventory().addItem(new ItemStack(landmine));
    231. econ.withdrawPlayer(p.getName(), getConfig().getDouble("landmineprice"));
    232. }
    233. }
    234. if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "WireCutters")){
    235. e.setCancelled(true);
    236. if(econ.getBalance(p.getName()) < getConfig().getDouble("wirecuttersprice")){
    237. p.sendMessage(ChatColor.RED + "You do not have enough money!");
    238. return;
    239. } else {
    240. ItemStack wirecutters = new ItemStack(Material.FLINT_AND_STEEL);
    241. ItemMeta wmeta = wirecutters.getItemMeta();
    242. wmeta.setDisplayName(ChatColor.GREEN + "WireCutters");
    243. wirecutters.setItemMeta(wmeta);
    244. p.getInventory().addItem(new ItemStack(wirecutters));
    245. econ.withdrawPlayer(p.getName(), getConfig().getDouble("landmineprice"));
    246. }
    247. }
    248. }
    249.  
    250. @EventHandler
    251. public void onPlayerInteract(PlayerInteractEvent e){
    252. Vector v1 = e.getPlayer().getEyeLocation().getDirection().multiply(2.0);
    253. if(e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
    254. Block b = e.getClickedBlock();
    255. if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Grenade")){
    256. e.setCancelled(true);
    257. Egg egg = e.getPlayer().getWorld().spawn(e.getPlayer().getEyeLocation().add(v1.getX(), v1.getY(), v1.getZ()), Egg.class);
    258. egg.setMetadata("grenade", new FixedMetadataValue(this, true));
    259. egg.setVelocity(v1);
    260. e.getPlayer().getInventory().remove(new ItemStack(Material.EGG, 1));
    261. }
    262. if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "LandMine")){
    263. e.setCancelled(true);
    264. Egg egg = e.getPlayer().getWorld().spawn(e.getPlayer().getEyeLocation().add(v1.getX(), v1.getY(), v1.getZ()), Egg.class);
    265. egg.setMetadata("landmine", new FixedMetadataValue(this, true));
    266. egg.setVelocity(v1);
    267. e.getPlayer().getInventory().remove(new ItemStack(Material.EGG, 1));
    268. }
    269. if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Bomb")){
    270. e.setCancelled(true);
    271. Egg egg = e.getPlayer().getWorld().spawn(e.getPlayer().getEyeLocation().add(v1.getX(), v1.getY(), v1.getZ()), Egg.class);
    272. egg.setMetadata("bomb", new FixedMetadataValue(this, true));
    273. egg.setVelocity(v1);
    274. e.getPlayer().getInventory().remove(new ItemStack(Material.EGG, 1));
    275. }
    276. if(bomb.contains(b.getLocation())){
    277. Player p = e.getPlayer();
    278. if(p.getItemInHand().getType() == Material.FLINT_AND_STEEL && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "WireCutters")){
    279. p.sendMessage(ChatColor.GREEN + "You have successfully defused the bomb!");
    280. Bukkit.getServer().getScheduler().cancelTasks(plugin);
    281. bomb.remove(b.getLocation());
    282. return;
    283. }
    284. }
    285. if(e.getClickedBlock().getType() != Material.SIGN && e.getClickedBlock().getType() != Material.WALL_SIGN
    286. && e.getClickedBlock().getType() != Material.SIGN_POST) return;
    287. System.out.println("Still Working");
    288. Sign s = (Sign) e.getClickedBlock().getState();
    289. if(s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + "Eggsplosions")){
    290. System.out.println("Works!");
    291. Player p = (Player) e.getPlayer();
    292. openInventory(p);
    293. }
    294. }
    295. }
    296.  


    Thanks for any help!
     
  2. Offline

    Rocoty

    If the player does not click a block, e.getClickedBlock() will return null
     
  3. Offline

    fireblast709

    PieMan456 If the Action is RIGHT_CLICK_AIR, then getClickedBlock() returns null :3
     
  4. Offline

    morshu9001

    They beat me to it.
     
  5. Offline

    PieMan456

Thread Status:
Not open for further replies.

Share This Page