NullPointerExecption

Discussion in 'Plugin Development' started by Gerov, Jun 23, 2014.

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

    Gerov

    Code:java
    1. @EventHandler
    2. public void chestOpen(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. Action action = e.getAction();
    5. if(action == action.RIGHT_CLICK_BLOCK){
    6. if(e.getClickedBlock() == plugin.savedBlock){
    7. if(plugin.savedBlock.getLocation() == plugin.locs){
    8. Bukkit.broadcastMessage(ChatColor.GREEN+"The chest has been found by "+p.getName());
    9. }
    10. }
    11. }
    12. }
    13.  
    14. }



    The exception is being caused by line 6.
     
  2. Offline

    Rocoty

    Okay
     
    AdamQpzm likes this.
  3. Offline

    Gerov

    So.... anyone going to help me?
     
  4. Offline

    Bavestry

    Gerov Want to show your main class?
     
  5. Offline

    Gerov

    Code:java
    1. package me.gerov.trea;
    2.  
    3. import java.io.File;
    4. import java.util.HashMap;
    5. import java.util.logging.Logger;
    6.  
    7. import me.gerov.listener.PListener;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Location;
    12. import org.bukkit.Material;
    13. import org.bukkit.World;
    14. import org.bukkit.block.Block;
    15. import org.bukkit.block.BlockState;
    16. import org.bukkit.block.Chest;
    17. import org.bukkit.command.Command;
    18. import org.bukkit.command.CommandSender;
    19. import org.bukkit.entity.Player;
    20. import org.bukkit.inventory.Inventory;
    21. import org.bukkit.inventory.ItemStack;
    22. import org.bukkit.plugin.PluginDescriptionFile;
    23. import org.bukkit.plugin.PluginManager;
    24. import org.bukkit.plugin.java.JavaPlugin;
    25. import org.bukkit.scheduler.BukkitRunnable;
    26.  
    27. public class main extends JavaPlugin {
    28. public final Logger logger = Logger.getLogger("Minecraft");
    29. public PluginManager pm;
    30. public Location locs;
    31. public Block savedBlock;
    32.  
    33.  
    34. @Override
    35. public void onDisable(){
    36. PluginDescriptionFile pdffile = this.getDescription();
    37. this.logger.info(pdffile.getName()+ChatColor.RED+" has been disabled!");
    38. this.getConfig();
    39. } @Override
    40. public void onEnable(){
    41. final main plugin = this;
    42. PluginDescriptionFile pdffile = this.getDescription();
    43. this.logger.info(pdffile.getName()+ChatColor.RED+" has been enabled!");
    44. File file = new File(getDataFolder()+File.separator+"config.yml");
    45.  
    46. if(!file.exists()){
    47. this.getLogger().info("Making config!");
    48. this.getConfig().addDefault("time_in_seconds_between_chest_spawn", 10000);
    49. this.getConfig().addDefault("max_x_distance", 500);
    50. this.getConfig().addDefault("max_z_distance", 500);
    51. this.getConfig().addDefault("world", "world");
    52. this.getConfig().addDefault("chestsSpawnNaturally", false);
    53. this.getConfig().options().copyDefaults(true);
    54. this.saveConfig();
    55. final int time = this.getConfig().getInt("time_in_seconds_between_chest_spawn");
    56. final int xDis = this.getConfig().getInt("max_x_distance");
    57. final int zDis = this.getConfig().getInt("max_z_distance");
    58. final String worldS = this.getConfig().getString("world");
    59. final boolean cn = this.getConfig().getBoolean("chestsSpawnNaturally");
    60. final World world = Bukkit.getWorld(worldS);
    61. if(cn == true){
    62. new BukkitRunnable() {
    63. int timer = 0;
    64. int nowtime = time*20;
    65. public void run() {
    66. if(timer == time*20){
    67. timer = 0;
    68. Location loc = new Location(world, 0,Math.random()*10,0);
    69. loc.setX( world.getSpawnLocation().getX() + Math.random() * xDis * 2 - xDis);
    70. loc.setZ( world.getSpawnLocation().getZ() + Math.random() * zDis * 2 - zDis);
    71. loc.setY( world.getHighestBlockAt(loc.getBlockX(), loc.getBlockZ() ).getY() );
    72. Location savedLocation = null;
    73. HashMap<Location, Location> locs = new HashMap<Location, Location>();
    74. locs.put(loc, savedLocation);
    75. loc.getBlock().setType(Material.CHEST);
    76. Block chest = loc.getBlock();
    77. savedBlock = null;
    78. HashMap<Block, Block> lchest = new HashMap<Block, Block>();
    79. lchest.put(chest, savedBlock);
    80. BlockState state = chest.getState();
    81. Inventory inv = ((Chest) state).getInventory();
    82. for(int x = 0; x <= 5;x++){
    83. int random = (int) (Math.random()*3);
    84. if(random <= 1){
    85. ItemStack strings = new ItemStack(Material.STRING,5);
    86. inv.addItem(strings);
    87. }else if(random <= 2){
    88. ItemStack diamond = new ItemStack(Material.DIAMOND,5);
    89. inv.addItem(diamond);
    90. }else if(random <= 3){
    91. ItemStack diamond = new ItemStack(Material.APPLE,10);
    92. inv.addItem(diamond);
    93. }
    94. }Bukkit.getServer().broadcastMessage(ChatColor.DARK_RED +"A chest has spawned at "+ChatColor.AQUA+loc.getX()+" "+ChatColor.AQUA+loc.getY()+" "+ChatColor.AQUA+loc.getZ()+"!");
    95. }else if(timer != nowtime){
    96. timer++;
    97. }
    98. }
    99. }.runTaskTimer(this, 0, 1);
    100. }else if(file.exists()){
    101. final int time1 = this.getConfig().getInt("time_in_seconds_between_chest_spawn");
    102. final int xDis1 = this.getConfig().getInt("max_x_distance");
    103. final int yDis = this.getConfig().getInt("max_y_distance");
    104. final int zDis1 = this.getConfig().getInt("max_z_distance");
    105. final String worldS1 = this.getConfig().getString("world");
    106. final World world1 = Bukkit.getWorld(worldS1);
    107. new BukkitRunnable() {
    108. int timer = 0;
    109. int nowtime = time1*20;
    110. public void run() {
    111. if(timer == time1*20){
    112. timer = 0;
    113. Location loc = new Location(world1, 0,Math.random()*10,0);
    114. loc.setX( world1.getSpawnLocation().getX() + Math.random() * xDis1 * 2 - xDis1); // This get a Random with a MaxRange
    115. loc.setZ( world1.getSpawnLocation().getZ() + Math.random() * zDis1 * 2 - zDis1);
    116. loc.setY( world1.getHighestBlockAt(loc.getBlockX(), loc.getBlockZ() ).getY() );
    117. loc.getBlock().setType(Material.CHEST);
    118. Block chest = loc.getBlock();
    119. pm = getServer().getPluginManager();
    120. PListener plis = new PListener();
    121. pm.registerEvents(plis, plugin);
    122. savedBlock = chest;
    123. locs = loc;
    124. BlockState state = chest.getState();
    125. Inventory inv = ((Chest) state).getInventory();
    126. for(int x = 0; x <= 5;x++){
    127. int random = (int) (Math.random()*3);
    128. if(random <= 1){
    129. ItemStack strings = new ItemStack(Material.STRING,5);
    130. inv.addItem(strings);
    131. }else if(random <= 2){
    132. ItemStack diamond = new ItemStack(Material.DIAMOND,5);
    133. inv.addItem(diamond);
    134. }else if(random <= 3){
    135. ItemStack diamond = new ItemStack(Material.APPLE,10);
    136. inv.addItem(diamond);
    137. }
    138. }Bukkit.getServer().broadcastMessage(ChatColor.DARK_RED +"A chest has spawned at "+ChatColor.AQUA+loc.getX()+" "+ChatColor.AQUA+loc.getY()+" "+ChatColor.AQUA+loc.getZ()+"!");
    139. }else if(timer != nowtime){
    140. timer++;
    141. }
    142. }
    143. }.runTaskTimer(this, 0, 1);
    144. }
    145. }
    146. }
    147. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    148. final int xDis = this.getConfig().getInt("max_x_distance");
    149. final int zDis = this.getConfig().getInt("max_z_distance");
    150. if(sender instanceof Player){
    151. Player player = (Player) sender;
    152. if(player.hasPermission("treasure.spawnchest")){
    153. if(commandLabel.equalsIgnoreCase("spawnchest")){
    154. Location loc = new Location(((Player) sender).getWorld(), 0,Math.random()*10,0);
    155. loc.setX( ((Player) sender).getWorld().getSpawnLocation().getX() + Math.random() * xDis * 2 - xDis); // This get a Random with a MaxRange
    156. loc.setZ( ((Player) sender).getWorld().getSpawnLocation().getZ() + Math.random() * zDis * 2 - zDis);
    157. loc.setY( ((Player) sender).getWorld().getHighestBlockAt(loc.getBlockX(), loc.getBlockZ() ).getY() );
    158. loc.getBlock().setType(Material.CHEST);
    159. Block chest = loc.getBlock();
    160.  
    161. BlockState state = chest.getState();
    162. Inventory inv = ((Chest) state).getInventory();
    163. for(int x = 0; x <= 5; x++){
    164. int random = (int)(Math.random() * 10) + 1;
    165. if(random == 1){
    166. ItemStack strings = new ItemStack(Material.STRING,5);
    167. inv.addItem(strings);
    168. }else if(random == 2){
    169. ItemStack diamond = new ItemStack(Material.DIAMOND,5);
    170. inv.addItem(diamond);
    171. }else if(random == 3){
    172. ItemStack diamond = new ItemStack(Material.APPLE,10);
    173. inv.addItem(diamond);
    174. }else if(random == 4){
    175. ItemStack gold = new ItemStack(Material.GOLD_INGOT,5);
    176. inv.addItem(gold);
    177. }else if(random == 5){
    178. ItemStack dirt = new ItemStack(Material.DIRT,10);
    179. inv.addItem(dirt);
    180. }else if(random == 6){
    181. ItemStack glowStone = new ItemStack(Material.GLOWSTONE,5);
    182. inv.addItem(glowStone);
    183. }else if(random == 7){
    184. ItemStack gApple = new ItemStack(Material.GOLDEN_APPLE,1);
    185. inv.addItem(gApple);
    186. }else if(random == 8){
    187. ItemStack ironPick = new ItemStack(Material.IRON_PICKAXE,1);
    188. inv.addItem(ironPick);
    189. }else if(random == 9){
    190. ItemStack diaHelm = new ItemStack(Material.DIAMOND_HELMET);
    191. inv.addItem(diaHelm);
    192. }else if(random == 10){
    193. ItemStack stone = new ItemStack(Material.STONE,32);
    194. inv.addItem(stone);
    195. }
    196. pm = getServer().getPluginManager();
    197. PListener plis = new PListener();
    198. final main plugin = this;
    199. pm.registerEvents(plis, plugin);
    200. savedBlock = chest;
    201. locs = loc;}Bukkit.getServer().broadcastMessage(ChatColor.DARK_RED +"A chest has spawned at "+ChatColor.AQUA+loc.getX()+" "+ChatColor.AQUA+loc.getY()+" "+ChatColor.AQUA+loc.getZ()+"! Spawned by "+player.getName()+".");
    202. }
    203. }
    204. }
    205. return true;}
    206. }


    I am testing with the command, ignore the 2 under the onEnable thing.

    Ehm...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  6. Offline

    2016mfransen

    your saved block is null, so either check that or make sure it is never null
     
  7. Offline

    AoH_Ruthless

    Gerov
    None of us are going to guess what your issue is for you ... You have to give us your full listener class, explain when the exception occurs .. Have you tried debugging?

    If you can't input effort, you can't accept effort in our responses.
     
    Bavestry likes this.
  8. Offline

    JustMe999

    well, you get a nullpointer cause you are not checking if the item is not null. use in the begin if(e.getitem != null).

    Code:java
    1. @EventHandler
    2. public void chestOpen(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. if(e.getitem != null){
    5. Action action = e.getAction();
    6. if(action == action.RIGHT_CLICK_BLOCK){
    7. if(e.getClickedBlock() == plugin.savedBlock){
    8. if(plugin.savedBlock.getLocation() == plugin.locs){
    9. Bukkit.broadcastMessage(ChatColor.GREEN+"The chest has been found by "+p.getName());
    10. }
    11. }
    12. }
    13. }
    14. }
    15.  
    16. }


    i hope this hellped a little bit, else sorry.
     
  9. Offline

    1Rogue

    No one has seemed to address it yet, so I'll just tell you that your "plugin" variable is null (I don't even see you passing it upon construction of PListener)

    .getItem() is irrelevant (not involved with the line throwing the npe at all
    The event isn't null
    Neither .getClickedBlock() nor .savedBlock is dereferenced here, so neither would throw an NPE.

    Also, you should note some of the basic java conventions (you don't need to compare a boolean value to true/false, classes should start with a capital, etc).
     
    Bavestry likes this.
Thread Status:
Not open for further replies.

Share This Page