Solved null pointer on a variable on load

Discussion in 'Plugin Development' started by nathanapes, Mar 25, 2014.

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

    nathanapes

    Hi I have an error.

    Could anyone help?

    Heres the what the code was pointing to:

    (I made a variable for config)

    double XP1 = config.getDouble("MobSpawnLocation);

    And so on.

    The variables are all the same except for the variable name and the string.

    This is how im using it:

    Location locP1 = new Location(name,XP1,YP1,ZP1);

    (name was defined somewhere else).

    And locP1 was being used by:

    World.spawnEntity(locP1, EntityType.ZOMBIE);

    Thats it.

    Can anybody tell me what in this variable caused this null pointer?

    Thanks.
     
  2. Offline

    Rocoty

    Post the class please. To track down the bug we don't just need immediately relevant code. We need context as well
     
  3. Offline

    nathanapes

    Rocoty ok i will do that when i get home.(i am in computer lab at school and i dont have my class file at the moment)
    XD
     
  4. Offline

    Rocoty

  5. Offline

    rfsantos1996

    On "caused by: " error message, you can see the line of the error, you just need to exam your code and pay attention for what caused it.

    Asking here is useless without:
    - the error
    - your source/code/source code

    Actually, asking here is 80% useless, you wrote your code, you know what you wrote and you are the only one who was writing, so you need to find what you wrote wrong, and you are who can do that (because you have the source and the error message).

    Basicly, you can easily search the error, this can seem big, but its helpful and reading is fun when you want to learn (;
     
  6. Offline

    nathanapes

    rfsantos1996 i know all that! :mad:

    Rocoty heres my class"

    Code:java
    1. package me.nathanapes;
    2.  
    3.  
    4.  
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Location;
    9. import org.bukkit.Material;
    10. import org.bukkit.World;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.configuration.file.FileConfiguration;
    14. import org.bukkit.entity.Creature;
    15. import org.bukkit.entity.Entity;
    16. import org.bukkit.entity.EntityType;
    17. import org.bukkit.entity.LivingEntity;
    18. import org.bukkit.entity.PigZombie;
    19. import org.bukkit.entity.Player;
    20. import org.bukkit.entity.Skeleton;
    21. import org.bukkit.event.Event;
    22. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    23. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    24. import org.bukkit.event.entity.EntityEvent;
    25. import org.bukkit.event.inventory.ClickType;
    26. import org.bukkit.event.inventory.InventoryClickEvent;
    27. import org.bukkit.event.inventory.InventoryType.SlotType;
    28. import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
    29. import org.bukkit.inventory.EntityEquipment;
    30. import org.bukkit.inventory.Inventory;
    31. import org.bukkit.inventory.ItemStack;
    32. import org.bukkit.plugin.Plugin;
    33. import org.bukkit.plugin.java.JavaPlugin;
    34. import org.bukkit.potion.PotionEffect;
    35. import org.bukkit.potion.PotionEffectType;
    36. import org.bukkit.scheduler.BukkitScheduler;
    37. import org.bukkit.scoreboard.Score;
    38. import org.bukkit.scoreboard.Scoreboard;
    39. import org.bukkit.scoreboard.Team;
    40. import org.bukkit.entity.Zombie;
    41.  
    42. @SuppressWarnings("unused")
    43. public class MobWars extends JavaPlugin{
    44. public void onEnable (){
    45. getLogger().info("Enabling!");
    46. getConfig().options().copyDefaults(true);
    47. config = getConfig();
    48. }
    49. public void onDisable () {
    50. getLogger().info("Disabling!");
    51. }
    52. static Player Player;
    53. static Team Team;
    54. static Plugin Plugin;
    55. static Entity Entity;
    56. static LivingEntity LivingEntity;
    57. static EntityEquipment EntityEquipment;
    58. static Inventory Inventory;
    59. static Scoreboard Scoreboard;
    60. static World World;
    61. static Score Score;
    62. static Creature Creature;
    63. static EntityEvent EntityEvent;
    64. static Zombie ze;
    65. static PigZombie pz;
    66. static Skeleton sk;
    67. static FileConfiguration config;
    68.  
    69. private boolean EntityDamageByEntityEvent(EntityType pigZombie,
    70. EntityType skeleton, DamageCause entityAttack, int i){
    71. return false;}
    72. boolean InventoryClickEvent(SlotType quickbar, int i, ClickType right){
    73. return false;}
    74. private void EntityRegainHealthEvent2(EntityType skeleton, int i) {
    75.  
    76. }
    77. private PotionEffect PotionEffect(PotionEffectType pet2, int i, int j) {
    78. return null;}
    79. private void EntityRegainHealthEvent(EntityType pigZombie, int i){
    80. }
    81. double XP1 = config.getDouble("MobSpawnLocationP1.X");
    82. double YP1 = config.getDouble("MobSpawnLocationP1.Y");
    83. double ZP1 = config.getDouble("MobSpawnLocationP1.Z");
    84.  
    85. double XP2 = config.getDouble("MobSpawnLocationP2.Z");
    86. double YP2 = config.getDouble("MobSpawnLocationP2.Y");
    87. double ZP2 = config.getDouble("MobSpawnLocationP2.Z");
    88.  
    89. double XP3 = config.getDouble("MobSpawnLocationP3.X");
    90. double YP3 = config.getDouble("MobSpawnLocationP3.Y");
    91. double ZP3 = config.getDouble("MobSpawnLocationP3.Z");
    92.  
    93. double XPl = config.getDouble("PlayerTPLocation.X");
    94. double YPl = config.getDouble("PlayerTPLocation.Y");
    95. double ZPl = config.getDouble("PlayerTPLocation.Z");
    96.  
    97. World w = Bukkit.getServer().getWorld(config.getString("MobWarsWorld"));
    98.  
    99.  
    100.  
    101. PotionEffectType pet = PotionEffectType.SLOW;
    102. PotionEffect pe = PotionEffect(pet,10000,100);
    103.  
    104.  
    105.  
    106. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    107. if (!(sender instanceof Player)) {
    108. sender.sendMessage("You started it on the Console.");
    109. }
    110. if(cmd.getName().equalsIgnoreCase("mwstart"));{
    111. if(sender.hasPermission("mw.mwstart")){
    112. if(Bukkit.getOnlinePlayers().length==3){
    113. BukkitScheduler scheduler5 = Bukkit.getServer().getScheduler();
    114. scheduler5.scheduleSyncDelayedTask((Plugin) this, new Runnable(){
    115. public void run() {
    116. Bukkit.broadcastMessage("Starting MobWars in 5" + ChatColor.RED);
    117. }}
    118. ,20L);
    119. BukkitScheduler scheduler4 = Bukkit.getServer().getScheduler();
    120. scheduler4.scheduleSyncDelayedTask((Plugin) this, new Runnable() {
    121. public void run() {
    122. Bukkit.broadcastMessage("Starting MobWars in 4" + ChatColor.RED);
    123. }}
    124. ,40L);
    125. BukkitScheduler scheduler3 = Bukkit.getServer().getScheduler();
    126. scheduler3.scheduleSyncDelayedTask((Plugin) this, new Runnable(){
    127. public void run() {
    128. Bukkit.broadcastMessage("Starting MobWars in 3" + ChatColor.RED);
    129. }}
    130. ,60L);
    131. BukkitScheduler scheduler2 = Bukkit.getServer().getScheduler();
    132. scheduler2.scheduleSyncDelayedTask((Plugin) this, new Runnable(){
    133. public void run() {
    134.  
    135. Bukkit.broadcastMessage("Starting MobWars in 2" + ChatColor.RED);
    136. }}
    137. ,80L);
    138. BukkitScheduler scheduler1 = Bukkit.getServer().getScheduler();
    139. scheduler1.scheduleSyncDelayedTask((Plugin) this, new Runnable(){
    140. public void run() {
    141. Bukkit.broadcastMessage("Starting MobWars in 1" + ChatColor.RED);
    142. }}
    143. ,100L);
    144. BukkitScheduler setupScheduler = Bukkit.getServer().getScheduler();
    145. setupScheduler.scheduleSyncDelayedTask((Plugin) this, new Runnable(){
    146. public void run(){
    147. Player.setAllowFlight(true);
    148. Player.setFlying(true);
    149. Player.teleport(new Location(w,XP1,YP1,ZP1),TeleportCause.PLUGIN);
    150. EntityEquipment.setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    151. EntityEquipment.setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    152. EntityEquipment.setBoots(new ItemStack(Material.IRON_BOOTS));
    153. EntityEquipment.setItemInHand(new ItemStack(Material.IRON_SWORD));
    154. EntityEquipment.setHelmet(new ItemStack(Material.IRON_HELMET));
    155. World.spawnEntity(new Location(w,XP1,YP1,ZP1),EntityType.ZOMBIE);
    156. World.spawnEntity(new Location(w,XP1,YP1,ZP1),EntityType.PIG_ZOMBIE);
    157. World.spawnEntity(new Location(w,XP2,YP2,ZP2),EntityType.SKELETON);
    158. if(InventoryClickEvent(SlotType.QUICKBAR,2,ClickType.RIGHT)){
    159. if(Scoreboard.getTeam("P3") != null){
    160. EntityRegainHealthEvent(EntityType.PIG_ZOMBIE,1);
    161. }}
    162. if(InventoryClickEvent(SlotType.QUICKBAR,2,ClickType.RIGHT)){
    163. if(Scoreboard.getTeam("P2") != null){
    164. EntityRegainHealthEvent(EntityType.SKELETON, 1);
    165. }}
    166. if(InventoryClickEvent(SlotType.QUICKBAR,2,ClickType.RIGHT)){
    167. if(Scoreboard.getTeam("P1") != null){
    168. EntityRegainHealthEvent(EntityType.ZOMBIE, 1);
    169. }}
    170. if (InventoryClickEvent(SlotType.QUICKBAR,1,ClickType.RIGHT)){
    171. if(Scoreboard.getTeam("P1") != null){
    172. if(LivingEntity.getCustomName().equals("SwordsManP1")){
    173. LivingEntity.addPotionEffect(pe,true);
    174. }}}
    175. if (InventoryClickEvent(SlotType.QUICKBAR,1,ClickType.RIGHT)){
    176. if(Scoreboard.getTeam("P2") != null){
    177. if(LivingEntity.getCustomName().equals("SwordsManP2")){
    178. LivingEntity.addPotionEffect(pe,true);
    179. }}}
    180. if (InventoryClickEvent(SlotType.QUICKBAR,1,ClickType.RIGHT)){
    181. if(Scoreboard.getTeam("P2") != null){
    182. if(LivingEntity.getCustomName().equals("SwordsManP2")){
    183. LivingEntity.addPotionEffect(pe,true);
    184. }}}
    185. if(EntityType.PIG_ZOMBIE != null){
    186. if(Scoreboard.getTeam("P3") != null){
    187. if(InventoryClickEvent(SlotType.QUICKBAR,0,ClickType.RIGHT)){
    188. if(LivingEntity.getCustomName().equals("SwordsManP3")){
    189. Creature.setTarget(ze);
    190. }}}}
    191. if(EntityType.PIG_ZOMBIE != null){
    192. if(Scoreboard.getTeam("P3") != null){
    193. if(InventoryClickEvent(SlotType.QUICKBAR,0,ClickType.RIGHT)){
    194. if(LivingEntity.getCustomName().equals("SwordsManP3")){
    195. Creature.setTarget(sk);
    196. }}}}
    197. if((EntityType.ZOMBIE) != null){
    198. if(Scoreboard.getTeam("P1") != null){
    199. if (InventoryClickEvent(SlotType.QUICKBAR,0,ClickType.RIGHT)){
    200. if(LivingEntity.getCustomName().equals("SwordsManP1")){
    201. Creature.setTarget(sk);
    202. }}}}
    203. if((EntityType.ZOMBIE) != null){
    204. if(Scoreboard.getTeam("P1") != null){
    205. if (InventoryClickEvent(SlotType.QUICKBAR,0,ClickType.RIGHT)){
    206. if(LivingEntity.getCustomName().equals("SwordsManP1")){
    207. Creature.setTarget(pz);
    208. }}}}
    209. if((EntityType.SKELETON) != null){
    210. if(Scoreboard.getTeam("P2") != null){
    211. if(LivingEntity.getCustomName().equals("SwordsManP2")){
    212. if(InventoryClickEvent(SlotType.QUICKBAR,0,ClickType.RIGHT)){
    213. Creature.setTarget(ze);
    214. }}}}
    215. if((EntityType.SKELETON) != null){
    216. if(Scoreboard.getTeam("P2") != null){
    217. if(LivingEntity.getCustomName().equals("SwordsManP2")){
    218. if(InventoryClickEvent(SlotType.QUICKBAR,0,ClickType.RIGHT)){
    219. Creature.setTarget(pz);
    220. }}}}
    221. Inventory.setItem(2,(new ItemStack(Material.REDSTONE_BLOCK)));
    222. Inventory.setItem(0,(new ItemStack(Material.IRON_SWORD)));
    223. Inventory.setItem(1,(new ItemStack(Material.WEB)));
    224. Scoreboard.registerNewTeam("P1");
    225. Scoreboard.registerNewTeam("P1");
    226. Scoreboard.registerNewTeam("P3");
    227. Team.addPlayer(Player);
    228. Team.addPlayer(Player);
    229. Team.addPlayer(Player);
    230. if(Scoreboard.getTeam("P1") != null){
    231. if(EntityType.ZOMBIE != null){
    232. LivingEntity.setCustomName("SwordsManP1");
    233. }}
    234. if(EntityType.ZOMBIE != null){
    235. if(Scoreboard.getTeam("P2") != null){
    236. LivingEntity.setCustomName("SwordsManP2");
    237. }}
    238. if(EntityType.ZOMBIE != null){
    239. if(Scoreboard.getTeam("P3")!= null){
    240. LivingEntity.setCustomName("SwordsManP3");
    241. }}
    242. Score.setScore(1);
    243. Score.setScore(1);
    244. Score.setScore(1);
    245. if(EntityDamageByEntityEvent(EntityType.ZOMBIE,(EntityType.SKELETON),DamageCause.ENTITY_ATTACK,20)){
    246. if(EntityDamageByEntityEvent(EntityType.ZOMBIE,(EntityType.PIG_ZOMBIE),DamageCause.ENTITY_ATTACK,20)){
    247. if(Scoreboard.getTeam("P1") != null){
    248. Score.setScore(+1);
    249. }}}
    250. if(EntityDamageByEntityEvent(EntityType.SKELETON,(EntityType.ZOMBIE),DamageCause.ENTITY_ATTACK,20)){
    251. if(EntityDamageByEntityEvent(EntityType.SKELETON,(EntityType.PIG_ZOMBIE),DamageCause.ENTITY_ATTACK,20)){
    252. if(Scoreboard.getTeam("P2") != null){
    253. Score.setScore(+1);
    254. }}}
    255. if(EntityDamageByEntityEvent(EntityType.PIG_ZOMBIE,EntityType.ZOMBIE,DamageCause.ENTITY_ATTACK,20)){
    256. if(Scoreboard.getTeam("P3") != null){
    257. if(EntityDamageByEntityEvent(EntityType.PIG_ZOMBIE,EntityType.SKELETON,DamageCause.ENTITY_ATTACK,20)){
    258. Score.setScore(+1);
    259. }}}
    260. if(World.getEntities().size()==0){
    261. if(EntityType.ZOMBIE != null){
    262. if(Scoreboard.getTeam("P1") != null){
    263. Player.kickPlayer("YOUR DEAD!");
    264. }}}
    265. if(World.getEntities().size()==0){
    266. if(EntityType.SKELETON != null){
    267. if(Scoreboard.getTeam("P2") != null){
    268. Player.kickPlayer("YOUR DEAD!");
    269. }}}
    270. if(World.getEntities().size()==0){
    271. if(EntityType.PIG_ZOMBIE != null){
    272. if(Scoreboard.getTeam("P3") != null){
    273. Player.kickPlayer("YOUR DEAD!");
    274. }}}
    275.  
    276. }
    277. }
    278. ,120L);
    279. }
    280. if(Bukkit.getOnlinePlayers().length==1){
    281. Bukkit.broadcastMessage("A Player has WON the Game!" + ChatColor.RED);
    282. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    283. public void run() {
    284. Player.kickPlayer("YOU WON!");
    285. Player.performCommand("mwstart");
    286. }}
    287. ,400L);
    288. Team.unregister();
    289. Team.unregister();
    290. Team.unregister();
    291. }
    292. }
    293. }
    294. return true;
    295. }
    296. }
    297.  
    298.  
    299.  
    300.  
    301.  
    302.  
    303.  
    304.  
    305.  
    306.  
    307.  
    308.  
    309.  
    310.  
    311.  
    312.  
    313.  
    314.  
    315.  
    316.  
    317.  
    318.  
    319.  
    320.  
    321.  
    322.  


    There you are!

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

    rfsantos1996

    Didn't meant to be rude, sorry, I read your post before you edit with more information.

    Your code is really strange looking and confusing. I can't tell where is the bug :confused:

    I would recommend you separate all methods and variables. I don't even know where you start, your onEnable just copy the config without setting any value into variables :/
     
  8. Offline

    nathanapes

    rfsantos1996 im sorry that think the code is messy.

    and what do you mean by copy the config?
     
  9. Offline

    rfsantos1996

    getConfig().options().copyDefaults(true);
    You basicly don't do nothing on your onEnable
     
  10. Offline

    nathanapes

  11. Offline

    Gater12

    rfsantos1996
    Ahh double negatives! ;)
    nathanapes
    I believe you are not creating the config, hence why it's null. Either call the method saveDefaultConfig() or do that and SAVE the config.
     
  12. Offline

    nathanapes

    Gater12 so how do you create the config???
     
  13. Offline

    Gater12

    nathanapes
    Either call saveDefaultConfig() or add saveConfig() after you copied the config.
     
  14. Offline

    nathanapes

    Gater12 but it still does it!!!!!
     
  15. Offline

    rfsantos1996

    Did you saw his code?

    I don't mean to be rude (really, I was like that when I was learning java), but its not messy, is A LOT MESSY. Like putting methods inside methods, I don't know how its compiled or he is sending the wrong code :confused:
     
  16. Offline

    nathanapes

  17. Offline

    Rocoty

    Right..so all those doubles are created and then trying to reference the config right after the class is instantiated...but before the plugin manager has had a chance to initialize the config. If you leave the declaration of the doubles where they are and move the initialization of them into onEnable you shouldn't see this error anymore

    rfsantos1996 I agree the code is messy and mostly incomprehensible but really there are no methods inside methods here. Where did you get that from?

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

    rfsantos1996

    Just he impression that it causes because of its spacing .-. I don't mean exacly that
     
  19. Offline

    nathanapes

    Rocoty so which method/variable has to moved to onEnable?
     
  20. Offline

    Rocoty

    Read my post above. For the doubles referencing getConfig(), you leave the declaration, but move the initialization to onEnable
     
  21. Offline

    nathanapes

    Rocoty what is initialization???

    Rocoty YAY IT FINALLY WORKED!

    -Thank you Rocoty!

    Solved!

    It worked cuz of what you told me to do.

    -Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page