Solved Keep getting NullPointerException in my coding

Discussion in 'Plugin Development' started by xxmobkiller, Sep 29, 2014.

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

    xxmobkiller

    Ok that works but now i got a bigger problem, When i use /arena setlobby arena it tells me NullPointerException setlobbycommand java line 14 LINE 14 =
    Code:java
    1. String arena = args[0];
    2. LINE 14 = ArenaManager.getManager().getArena(arena).setLobbySpawn(arena, p.getLocation());

    and here what ArenaManager class is
    Code:java
    1.  
    2. private static HashMap<String,Arena> arenas = new HashMap<String,Arena>();
    3. public static ArenaManager am = new ArenaManager();
    4.  
    5. private ArenaManager() {}
    6.  
    7. public static ArenaManager getManager() {
    8. return am;
    9. }
    10.  
    11. public Arena getArena(String i){
    12. for(Arena a : arenas.values()){
    13. if(a.getArenaName() == i){
    14. return a;
    15. }
    16. }
    17. return null;
    18. }
    19.  
    20. public static void loadArenas() {
    21. arenas = new HashMap<String,Arena>();
    22. File directory = new File(TNTWars.getInstance().getDataFolder(), "/arenas/");
    23. if (!directory.exists() || !directory.isDirectory())
    24. return;
    25. for (File f : directory.listFiles()) {
    26. String name = f.getName();
    27. if (name.contains(".yml")) {
    28. String arena = name.replace(".yml", "");
    29. try {
    30. Arena a = new Arena(arena);
    31. arenas.put(arena, a);
    32. } catch (Exception e) {
    33. e.printStackTrace();
    34. }
    35. }
    36. }
    37. }
    38.  
    39.  
    40. CLASS FOR ARENA
    41.  
    42.  
    43. private ArrayList<String> inGamePlayers = new ArrayList<String>();
    44. private ArrayList<String> blueTeam = new ArrayList<String>();
    45. private ArrayList<String> specTeam = new ArrayList<String>();
    46. private ArrayList<String> redTeam = new ArrayList<String>();
    47. private int maxplayers;
    48. private int minplayers;
    49. private boolean inGame;
    50. private Location lobby;
    51. private Location blue;
    52. private Location spec;
    53. private Location red;
    54. private String name;
    55.  
    56. public Arena(String name) {
    57.  
    58. this.name = name;
    59. this.maxplayers = 8;
    60. this.minplayers = 2;
    61. this.inGame = false;
    62. this.lobby = new Location(Bukkit.getWorld("world"), 0.0D, 0.0D, 0.0D, 0F, 0F);
    63. this.blue = new Location(Bukkit.getWorld("world"), 0.0D, 0.0D, 0.0D, 0F, 0F);
    64. this.red = new Location(Bukkit.getWorld("world"), 0.0D, 0.0D, 0.0D, 0F, 0F);
    65. this.spec = new Location(Bukkit.getWorld("world"), 0.0D, 0.0D, 0.0D, 0F, 0F);
    66.  
    67. TNTWars.ac.getDataConfig(name).set("Arena.Name", name);
    68. TNTWars.ac.getDataConfig(name).set("Arena.Schematic", name);
    69. TNTWars.ac.getDataConfig(name).set("Arena.Countdown", 15);
    70. TNTWars.ac.getDataConfig(name).set("Arena.MaxPlayers", maxplayers);
    71. TNTWars.ac.getDataConfig(name).set("Arena.MinPlayers", minplayers);
    72. TNTWars.ac.getDataConfig(name).set("Arena.Protection.Pos1", "POS1");
    73. TNTWars.ac.getDataConfig(name).set("Arena.Protection.Pos2", "POS2");
    74.  
    75. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Lobby.World", lobby.getWorld().getName());
    76. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Lobby.X", lobby.getBlockX());
    77. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Lobby.Y", lobby.getBlockY());
    78. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Lobby.Z", lobby.getBlockZ());
    79. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Lobby.Yaw", lobby.getYaw());
    80. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Lobby.Pitch", lobby.getPitch());
    81.  
    82. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Blue.World", blue.getWorld().getName());
    83. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Blue.X", blue.getBlockX());
    84. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Blue.Y", blue.getBlockY());
    85. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Blue.Z", blue.getBlockZ());
    86. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Blue.Yaw", blue.getYaw());
    87. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Blue.Pitch", blue.getPitch());
    88.  
    89. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Red.World", red.getWorld().getName());
    90. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Red.X", red.getBlockX());
    91. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Red.Y", red.getBlockY());
    92. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Red.Z", red.getBlockZ());
    93. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Red.Yaw", red.getYaw());
    94. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Red.Pitch", red.getPitch());
    95.  
    96. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Spec.World", spec.getWorld().getName());
    97. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Spec.X", spec.getBlockX());
    98. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Spec.Y", spec.getBlockY());
    99. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Spec.Z", spec.getBlockZ());
    100. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Spec.Yaw", spec.getYaw());
    101. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Spec.Pitch", spec.getPitch());
    102.  
    103. TNTWars.ac.saveDataConfig(name);
    104. }
    105.  
    106. public String getArenaName() {
    107. return this.name;
    108. }
    109.  
    110. public int getMaxPlayers() {
    111. return this.maxplayers;
    112. }
    113.  
    114. public int getMinPlayers() {
    115. return this.minplayers;
    116. }
    117.  
    118. public void setInGame(boolean ingame) {
    119. this.inGame = ingame;
    120. }
    121.  
    122. public boolean isInGame() {
    123. return this.inGame;
    124. }
    125.  
    126. public void setMaxPlayers(String name, int max) {
    127. this.maxplayers = max;
    128. TNTWars.ac.getDataConfig(name).set("Arena.MaxPlayers", max);
    129. TNTWars.ac.saveDataConfig(name);
    130. }
    131.  
    132. public void setMinPlayers(String name, int min) {
    133. this.minplayers = min;
    134. TNTWars.ac.getDataConfig(name).set("Arena.MinPlayers", min);
    135. TNTWars.ac.saveDataConfig(name);
    136. }
    137.  
    138. public void setSpecSpawn(String name, Location l) {
    139. this.spec = l;
    140. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Spec.World", spec.getWorld().getName());
    141. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Spec.X", spec.getBlockX());
    142. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Spec.Y", spec.getBlockY());
    143. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Spec.Z", spec.getBlockZ());
    144. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Spec.Yaw", spec.getYaw());
    145. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Spec.Pitch", spec.getPitch());
    146. TNTWars.ac.saveDataConfig(name);
    147. }
    148.  
    149. public Location getSpecSpawn() {
    150. return this.spec;
    151. }
    152.  
    153. public void setBlueSpawn(String name, Location l) {
    154. this.blue = l;
    155. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Blue.World", blue.getWorld().getName());
    156. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Blue.X", blue.getBlockX());
    157. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Blue.Y", blue.getBlockY());
    158. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Blue.Z", blue.getBlockZ());
    159. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Blue.Yaw", blue.getYaw());
    160. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Blue.Pitch", blue.getPitch());
    161. TNTWars.ac.saveDataConfig(name);
    162. }
    163.  
    164. public void setRedSpawn(String name, Location l) {
    165. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Red.World", l.getWorld().getName());
    166. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Red.X", l.getBlockX());
    167. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Red.Y", l.getBlockY());
    168. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Red.Z", l.getBlockZ());
    169. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Red.Yaw", l.getYaw());
    170. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Red.Pitch", l.getPitch());
    171. TNTWars.ac.saveDataConfig(name);
    172. this.red = l;
    173. }
    174.  
    175. public void setLobbySpawn(String name, Location l) {
    176. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Lobby.World", l.getWorld().getName());
    177. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Lobby.X", l.getBlockX());
    178. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Lobby.Y", l.getBlockY());
    179. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Lobby.Z", l.getBlockZ());
    180. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Lobby.Yaw", l.getYaw());
    181. TNTWars.ac.getDataConfig(name).set("Arena.Spawns.Lobby.Pitch", l.getPitch());
    182. TNTWars.ac.saveDataConfig(name);
    183. this.lobby = l;
    184. }
    185.  
    186. public Location getBlueSpawn() {
    187. return this.blue;
    188. }
    189.  
    190. public Location getRedSpawn() {
    191. return this.red;
    192. }
    193.  
    194. public Location getLobbySpawn() {
    195. return this.lobby;
    196. }
    197.  
    198. public List<String> getRedTeam() {
    199. return this.redTeam;
    200. }
    201.  
    202. public List<String> getBlueTeam() {
    203. return this.blueTeam;
    204. }
    205.  
    206. public List<String> getSpecTeam() {
    207. return this.specTeam;
    208. }
    209.  
    210. public List<String> getInGamePlayers() {
    211. return this.inGamePlayers;
    212. }
    213.  


    Yea i recoded the arena class it now check to see if the arena file exist and if it does then just load its config if not then it will just create its self a config. But it seems that ArenaManager is not working when i try to call it same with any other commands the only command that works when calling ArenaManager is /arena create arena only problem is when i try to set a spawn it gives me a null pointer about ArenaManager line 14 that all it says

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  2. xxmobkiller you should always include "null safety". that means you insert everywhere like there null checks instead of calling methods on objects you don't know if they exist. It would look like this with a nullcheck:
    Code:java
    1. Arena a = ArenaManager.getManager().getArena(arena);
    2. if(a!=null){
    3. // Do whatever you want with your arena, it does exist
    4. }else{
    5. // Your arena does not exist. Handle this somehow
    6. }

    It will make a few lines more but null safety is very important for good and error free code :)

    EDIT: And baaaaooom, i did the same error :D you should also check if your getManager() method of your ArenaManager class returns null. This could also cause a nullpointer exception because you call on the object it should return getArena();
     
  3. Offline

    xxmobkiller

    Ok than kyou man, Been trying to fix the problem for over 6 hours now i even recoded the plugin i thought it was class that was messing up any ways THANK YOU Shmobi

    Arena keeps returning null


    Code:java
    1. public Arena getArena(String i){
    2. for(Arena a : arenas.values()){
    3. if(a.getArenaName() == i){
    4. return a;
    5. }
    6. }
    7. return null;
    8. }


    I have not clue why but its just keep returning null because I did what you told me to do was to check to see if arena return null or ArenaManager well arena is the one that keeps returning null, Any ideas how i would go about making not return null?

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

    fireblast709

    xxmobkiller Don't compare Strings with ==, use .equals()
     
  5. Offline

    xxmobkiller

    Now my Config will not work its keep using the old name to create a new file so if i type /arena create test it wil create file test.yml then if i type /arena create test2 it replace the test.yml in side it with my location when i used /arena create test2 i want it to create each arena its own config file.yml

    Code:java
    1. public class ArenasConfig implements Listener {
    2.  
    3. public FileConfiguration StorageFilef = null;
    4. private File customConfigFile = null;
    5. private TNTWars plugin;
    6.  
    7. public ArenasConfig(TNTWars plugin) {
    8. this.plugin = plugin;
    9. }
    10.  
    11. @SuppressWarnings("deprecation")
    12. public void reloadDataConfig(String name) {
    13. if (customConfigFile == null) {
    14. customConfigFile = new File(plugin.getDataFolder() + File.separator + "arenas" + File.separator, name + ".yml");
    15. }
    16. StorageFilef = YamlConfiguration.loadConfiguration(customConfigFile);
    17. InputStream defConfigStream = plugin.getResource("/arenas/" + name + ".yml");
    18. if (defConfigStream != null) {
    19. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    20. StorageFilef.setDefaults(defConfig);
    21. }
    22. }
    23.  
    24. public FileConfiguration getDataConfig(String name) {
    25. if (StorageFilef == null) {
    26. this.reloadDataConfig(name);
    27. }
    28. return StorageFilef;
    29. }
    30.  
    31. public void saveDataConfig(String name) {
    32. if (StorageFilef == null || customConfigFile == null) {
    33. return;
    34. }
    35. try {
    36. getDataConfig(name).save(customConfigFile);
    37. } catch (IOException ex) {
    38. TNTWars.getInstance().getLogger().log(Level.SEVERE,"Could not save config to " + customConfigFile, ex);
    39. }
    40. }
    41.  
    42. }


    Anyone can help me ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  6. xxmobkiller this thread is marked as solved, you should undo this to make sure people will read your thread again
     
  7. Offline

    xxmobkiller

Thread Status:
Not open for further replies.

Share This Page