Solved NPE With Custom YML File

Discussion in 'Plugin Development' started by krazytraynz, Jul 15, 2013.

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

    krazytraynz

    I know there's millions of these topics, but I just couldn't take information out of them that could help solve this. I'm trying to create a system that writes to the file after a Player kills an entity/breaks a block, but whenever the event is called I get a NPE.

    Error:
    Code:
    01:13:24 [SEVERE] Could not pass event EntityDeathEvent to HardcoreMode v2.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callEntityDeat
    hEvent(CraftEventFactory.java:329)
            at net.minecraft.server.v1_6_R2.EntityPig.dropDeathLoot(EntityPig.java:1
    08)
            at net.minecraft.server.v1_6_R2.EntityLiving.die(EntityLiving.java:735)
            at net.minecraft.server.v1_6_R2.EntityLiving.damageEntity(EntityLiving.j
    ava:687)
            at net.minecraft.server.v1_6_R2.EntityAnimal.damageEntity(SourceFile:128
    )
            at net.minecraft.server.v1_6_R2.EntityHuman.attack(EntityHuman.java:874)
     
            at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java
    :1115)
            at net.minecraft.server.v1_6_R2.Packet7UseEntity.handle(SourceFile:36)
            at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296
    )
            at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java
    :118)
            at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    90)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    26)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.NullPointerException
            at com.github.KrazyTraynz.PlayerScoresHandler.reloadYml(PlayerScoresHand
    ler.java:24)
            at com.github.KrazyTraynz.HardcoreScoreHandler.onKill(HardcoreScoreHandl
    er.java:76)
            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.ja
    va:425)
            ... 20 more
    PlayerScoresHandler (Lines pointed to by error labeled):
    Code:java
    1. package com.github.KrazyTraynz;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.util.logging.Level;
    7.  
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.configuration.file.YamlConfiguration;
    10.  
    11. public class PlayerScoresHandler {
    12.  
    13. private HardcoreMode hc;
    14.  
    15. private FileConfiguration yml = null;
    16. private File ymlFile = null;
    17.  
    18. public PlayerScoresHandler(HardcoreMode hc){
    19. this.hc = hc;
    20. }
    21.  
    22. public void reloadYml() {
    23. if (ymlFile == null) {
    24. ymlFile = new File(hc.getDataFolder(), "PlayerScores.yml"); //error
    25. }
    26. yml = YamlConfiguration.loadConfiguration(ymlFile);
    27.  
    28. InputStream defConfigStream = hc.getResource("PlayerScores.yml");
    29. if (defConfigStream != null) {
    30. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    31. yml.setDefaults(defConfig);
    32. }
    33. }
    34.  
    35. public FileConfiguration getYml() {
    36. if (yml == null) {
    37. this.reloadYml();
    38. }
    39. return yml;
    40. }
    41.  
    42. public void saveCustomConfig() {
    43. if (yml == null || ymlFile == null) {
    44. return;
    45. }
    46. try {
    47. getYml().save(ymlFile);
    48. } catch (IOException ex) {
    49. hc.getLogger().log(Level.SEVERE, "Could notthis.save config to " + ymlFile, ex);
    50. }
    51. }
    52.  
    53. public void addScore(String s, int toAdd){
    54. int finalScore = yml.getInt(s);
    55. finalScore = finalScore + toAdd;
    56. yml.set(s, finalScore);
    57. this.saveCustomConfig();
    58. }
    59.  
    60. public void deleteScorePath(String s){
    61. yml.set(s, null);
    62. this.saveCustomConfig();
    63. }
    64.  
    65. public void createPath(String s){
    66. yml.set(s, "0");
    67. this.saveCustomConfig();
    68. }
    69.  
    70. public int checkScoreFile(String s){
    71. int i = yml.getInt(s);
    72. return i;
    73. }
    74.  
    75. public void editScoreFile(String s, int i){
    76. yml.set(s, i);
    77. this.saveCustomConfig();
    78. }
    79.  
    80. public int getMineInt(String s){
    81. int i = 0;
    82. i = hc.getConfig().getInt("Mining." + s);
    83. return i;
    84. }
    85.  
    86. public int getMobInt(String s){
    87. int i = 0;
    88. i = hc.getConfig().getInt("MobKill." + s);
    89. return i;
    90. }
    91.  
    92. public int getBossInt(String s){
    93. int i = 0;
    94. i = hc.getConfig().getInt("BossKill." + s);
    95. return i;
    96. }
    97.  
    98. public int getAnimalInt(String s){
    99. int i = 0;
    100. i = hc.getConfig().getInt("AnimalKill." + s);
    101. return i;
    102. }
    103.  
    104. public int getMiscInt(String s){
    105. int i = 0;
    106. i = hc.getConfig().getInt("MiscKill." + s);
    107. return i;
    108. }
    109. }


    HardcoreScoreHandler:
    Code:java
    1. package com.github.KrazyTraynz;
    2.  
    3. import net.minecraft.server.v1_6_R2.Block;
    4.  
    5. import org.bukkit.entity.Entity;
    6. import org.bukkit.entity.EntityType;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.entity.Skeleton;
    9. import org.bukkit.entity.Skeleton.SkeletonType;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.BlockBreakEvent;
    13. import org.bukkit.event.entity.EntityDeathEvent;
    14.  
    15. public class HardcoreScoreHandler implements Listener{
    16.  
    17. private HardcoreMode hc;
    18.  
    19. public HardcoreScoreHandler(HardcoreMode hc){
    20. this.hc = hc;
    21. }
    22.  
    23. PlayerScoresHandler psh = new PlayerScoresHandler(hc);
    24.  
    25. int score;
    26.  
    27. @EventHandler
    28. public void onPlayerMine(BlockBreakEvent e){
    29. if(e.getPlayer() != null){
    30.  
    31. Player p = e.getPlayer();
    32. psh.createPath(p.getName());
    33.  
    34. psh.reloadYml();
    35.  
    36. score = psh.checkScoreFile(p.getName());
    37.  
    38. if(e.getBlock() == Block.DIAMOND_ORE){
    39. psh.addScore(p.getName(), psh.getMineInt("Diamond"));
    40. psh.editScoreFile(p.getName(), score);
    41. }
    42.  
    43. else if(e.getBlock() == Block.EMERALD_ORE){
    44. psh.addScore(p.getName(), psh.getMineInt("Emerald"));
    45. psh.editScoreFile(p.getName(), score);
    46. }
    47.  
    48. else if(e.getBlock() == Block.COAL_ORE){
    49. psh.addScore(p.getName(), psh.getMineInt("Coal"));
    50. psh.editScoreFile(p.getName(), score);
    51. }
    52.  
    53. else if(e.getBlock() == Block.REDSTONE_ORE){
    54. psh.addScore(p.getName(), psh.getMineInt("Redstone"));
    55. psh.editScoreFile(p.getName(), score);
    56. }
    57.  
    58. else if(e.getBlock() == Block.LAPIS_ORE){
    59. psh.addScore(p.getName(), psh.getMineInt("Lapis"));
    60. psh.editScoreFile(p.getName(), score);
    61. }
    62.  
    63. else if(e.getBlock() == Block.MOB_SPAWNER){
    64. psh.addScore(p.getName(), psh.getMineInt("Spawner"));
    65. psh.editScoreFile(p.getName(), score);
    66. }
    67. else{
    68. return;
    69. }
    70. }
    71. }
    72.  
    73. @EventHandler
    74. public void onKill(EntityDeathEvent e){
    75. Entity ent = e.getEntity().getKiller();
    76. if(ent != null && ent instanceof Player){
    77. Player p = e.getEntity().getKiller();
    78.  
    79. psh.reloadYml();//error
    80.  
    81. psh.createPath(p.getName());
    82.  
    83. score = psh.checkScoreFile(p.getName());
    84.  
    85. if(e.getEntityType() == EntityType.BLAZE){
    86. psh.addScore(p.getName(), psh.getMobInt("Blaze"));
    87. psh.editScoreFile(p.getName(), score);
    88. }
    89.  
    90. else if(e.getEntityType() == EntityType.CAVE_SPIDER){
    91. psh.addScore(p.getName(), psh.getMobInt("CaveSpider"));
    92. psh.editScoreFile(p.getName(), score);
    93. }
    94.  
    95. else if(e.getEntityType() == EntityType.CREEPER){
    96. psh.addScore(p.getName(), psh.getMobInt("Creeper"));
    97. psh.editScoreFile(p.getName(), score);
    98. }
    99.  
    100. else if(e.getEntityType() == EntityType.ENDERMAN){
    101. psh.addScore(p.getName(), psh.getMobInt("Enderman"));
    102. psh.editScoreFile(p.getName(), score);
    103. }
    104.  
    105. else if(e.getEntityType() == EntityType.GHAST){
    106. psh.addScore(p.getName(), psh.getMobInt("Ghast"));
    107. psh.editScoreFile(p.getName(), score);
    108. }
    109.  
    110. else if(e.getEntityType() == EntityType.MAGMA_CUBE){
    111. psh.addScore(p.getName(), psh.getMobInt("MagmaCube"));
    112. psh.editScoreFile(p.getName(), score);
    113. }
    114.  
    115. else if(e.getEntityType() == EntityType.PIG_ZOMBIE){
    116. psh.addScore(p.getName(), psh.getMobInt("PigZombie"));
    117. psh.editScoreFile(p.getName(), score);
    118. }
    119.  
    120. else if(e.getEntityType() == EntityType.SKELETON){
    121. Skeleton skele = (Skeleton) e.getEntity();
    122. if(skele.getSkeletonType() == SkeletonType.WITHER){
    123. psh.addScore(p.getName(), psh.getMobInt("WitherSkeleton"));
    124. psh.editScoreFile(p.getName(), score);
    125. }
    126. else{
    127. psh.addScore(p.getName(), psh.getMobInt("Skeleton"));
    128. psh.editScoreFile(p.getName(), score);
    129. }
    130. }
    131.  
    132. else if(e.getEntityType() == EntityType.SLIME){
    133. psh.addScore(p.getName(), psh.getMobInt("Slime"));
    134. psh.editScoreFile(p.getName(), score);
    135. }
    136.  
    137. else if(e.getEntityType() == EntityType.SPIDER){
    138. psh.addScore(p.getName(), psh.getMobInt("Spider"));
    139. psh.editScoreFile(p.getName(), score);
    140. }
    141.  
    142. else if(e.getEntityType() == EntityType.ZOMBIE){
    143. psh.addScore(p.getName(), psh.getMobInt("Zombie"));
    144. psh.editScoreFile(p.getName(), score);
    145. }
    146.  
    147.  
    148. else if(e.getEntityType() == EntityType.ENDER_DRAGON){
    149. psh.addScore(p.getName(), psh.getBossInt("EnderDragon"));
    150. psh.editScoreFile(p.getName(), score);
    151. }
    152.  
    153. else if(e.getEntityType() == EntityType.WITHER){
    154. psh.addScore(p.getName(), psh.getBossInt("WitherBoss"));
    155. psh.editScoreFile(p.getName(), score);
    156. }
    157.  
    158.  
    159. else if(e.getEntityType() == EntityType.CHICKEN){
    160. psh.addScore(p.getName(), psh.getAnimalInt("Chicken"));
    161. psh.editScoreFile(p.getName(), score);
    162. }
    163.  
    164. else if(e.getEntityType() == EntityType.COW){
    165. psh.addScore(p.getName(), psh.getAnimalInt("Cow"));
    166. psh.editScoreFile(p.getName(), score);
    167. }
    168.  
    169. else if(e.getEntityType() == EntityType.PIG){
    170. psh.addScore(p.getName(), psh.getAnimalInt("Pig"));
    171. psh.editScoreFile(p.getName(), score);
    172. }
    173.  
    174. else if(e.getEntityType() == EntityType.SHEEP){
    175. psh.addScore(p.getName(), psh.getAnimalInt("Sheep"));
    176. psh.editScoreFile(p.getName(), score);
    177. }
    178.  
    179. else if(e.getEntityType() == EntityType.VILLAGER){
    180. psh.addScore(p.getName(), psh.getMiscInt("Villager"));
    181. psh.editScoreFile(p.getName(), score);
    182. }
    183. }
    184. }
    185.  
    186. //TODO playerdeath, getkiller
    187. }
     
  2. Offline

    Nitnelave

    Are you sure that you pass a non-null object to the HardcoreScoreHandler constructor?
     
  3. Offline

    krazytraynz

    HardcoreMode is the main plugin class, so it shouldn't be null.
     
  4. Offline

    amitlin14

    krazytraynz, initiate PlayerScoreHandler inside the constructor of HardcoreScoreHandler, my guess is that because it is initialized outside the constructor, when hc is still null, it recieves a null value in its HardcoreMode hc field, which in turn, when you use hc.getDataFolder(), causes an NPE error, since you are trying to get values from methods inside a null object.

    To fix this simply initiate playerScoreHandler inside HardcoreScoreHandler.
     
  5. Offline

    krazytraynz

    That fixed it, thanks :D
     
Thread Status:
Not open for further replies.

Share This Page