Plugin isn't enabling -- Null Pointer Exception

Discussion in 'Plugin Development' started by Caprei, Oct 24, 2014.

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

    Caprei

    Hey all. I'm trying to create a plugin, however it's having trouble enabling. Here's the important parts of the stack trace:
    Code:
    java.lang.NullPointerException
            at me.Caprei.PvPRanks.PvPListener.<init>(PvPListener.java:21) ~[?:?]
            at me.Caprei.PvPRanks.PvPRanks.onEnable(PvPRanks.java:21) ~[?:?]
    
    Here is the first class:

    Code:java
    1. package me.Caprei.PvPRanks;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class PvPRanks extends JavaPlugin{
    14.  
    15. HashMap<Integer, Integer> levels = new HashMap<Integer, Integer>();
    16. FileConfiguration config = getConfig();
    17. private boolean found = false;
    18. int level;
    19.  
    20. public void onEnable(){
    21. Bukkit.getServer().getPluginManager().registerEvents(new PvPListener(this), this);
    22. getConfig().options().copyDefaults(true);
    23. int x = 1;
    24. while(x < 28){
    25. levels.put(x, (int) Math.pow(config.getInt("Initial amount of points needed between first and second level"), config.getDouble("Multiplier between levels (will be rounded)")));
    26. }
    27. getServer().getLogger().info("HashMap containing all values for each level has now been loaded. The multiplier between levels is: " + config.getDouble("Multiplier between levels (will be rounded)" + ". This can be changed in the config."));
    28. }
    29.  
    30. public void onDisable(){
    31.  
    32. }
    33.  
    34. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String [] args){
    35. if(cmd.getName().equalsIgnoreCase("pvp")){
    36. if(args.length < 2){
    37. sender.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "All PvP commands require at LEAST two arguments of the form: /pvp <command> <playername>");
    38. }
    39.  
    40.  
    41.  
    42. if(args[0].equalsIgnoreCase("rankup")){
    43. if(args[2] == null){
    44. if(!(sender.hasPermission("pvpr.rankup"))){
    45. sender.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have permission to do that.");
    46. return true;
    47. }
    48. String name = args[1];
    49.  
    50. for(Player player:Bukkit.getOnlinePlayers()){
    51. if(player.getName().equalsIgnoreCase(name)){
    52. player.sendMessage(ChatColor.RED + ChatColor.BOLD.toString() + "You were promoted by: " + sender);
    53. int x = 1;
    54. while(x < 28){
    55. int value = (int) levels.get(x);
    56. if(config.getInt("players." + player.getUniqueId().toString()) < value){
    57. level = levels.get(x);
    58. }
    59. }
    60. config.set("players." + player.getUniqueId().toString(), config.getInt("players." + player.getUniqueId().toString()) + (level - config.getInt("players." + player.getUniqueId().toString())));
    61. found = true;
    62. break;
    63. }
    64. }
    65. if(found == false){
    66. sender.sendMessage(ChatColor.RED + ChatColor.BOLD.toString() + "Couldn't find player: " + args[1] + "!");
    67. }else{
    68. found = false;
    69. }
    70.  
    71.  
    72. }
    73.  
    74. return true;
    75. }
    76.  
    77.  
    78.  
    79. else if(args[0].equalsIgnoreCase("rankdown")){
    80. if(args[2] == null){
    81. if(!(sender.hasPermission("pvpr.rankdown"))){
    82. sender.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have permission to do that.");
    83. return true;
    84. }
    85. String name = args[1];
    86.  
    87. for(Player player:Bukkit.getOnlinePlayers()){
    88. if(player.getName().equalsIgnoreCase(name)){
    89. player.sendMessage(ChatColor.RED + ChatColor.BOLD.toString() + "You were demoted by: " + sender);
    90. int x = 1;
    91. while(x < 28){
    92. int value = (int) levels.get(x);
    93. if(config.getInt("players." + player.getUniqueId().toString()) < value){
    94. level = levels.get(x - 2);
    95. }
    96. }
    97. config.set("players." + player.getUniqueId().toString(), config.getInt("players." + player.getUniqueId().toString()) - (config.getInt("players." + player.getUniqueId().toString()) - level));
    98. found = true;
    99. break;
    100. }
    101. }
    102. if(found == false){
    103. sender.sendMessage(ChatColor.RED + ChatColor.BOLD.toString() + "Couldn't find player: " + args[1] + "!");
    104. }else{
    105. found = false;
    106. }
    107. }
    108. return true;
    109. }
    110.  
    111.  
    112. else if(args[0].equalsIgnoreCase("rankup")){
    113. if(!(sender.hasPermission("pvpr.setrank"))){
    114. sender.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have permission to do that.");
    115. return true;
    116. }
    117. String name = args[1];
    118. Integer rank = Integer.parseInt(args[2]);
    119.  
    120. for(Player player:Bukkit.getOnlinePlayers()){
    121. if(player.getName().equalsIgnoreCase(name)){
    122. if(levels.containsKey(rank)){
    123. int needed = levels.get(rank);
    124. config.set("players." + player.getUniqueId().toString(), config.getInt("players." + player.getUniqueId().toString()) + (needed - config.getInt("players." + player.getUniqueId().toString())));
    125.  
    126. }else{
    127. sender.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "Couldn't find that rank.");
    128. }
    129. config.set("players." + player.getUniqueId().toString(), config.getInt("players." + player.getUniqueId().toString()) - (level - config.getInt("players." + player.getUniqueId().toString())));
    130. found = true;
    131. break;
    132. }
    133. }
    134. if(found == false){
    135. sender.sendMessage(ChatColor.RED + ChatColor.BOLD.toString() + "Couldn't find player: " + args[1] + "!");
    136. }else{
    137. found = false;
    138. }
    139. return true;
    140. }
    141.  
    142. else if(args[0].equalsIgnoreCase("rankdown")){
    143. if(!(sender.hasPermission("pvpr.setrank"))){
    144. sender.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have permission to do that.");
    145. return true;
    146. }
    147. String name = args[1];
    148. Integer rank = Integer.parseInt(args[2]);
    149.  
    150. for(Player player:Bukkit.getOnlinePlayers()){
    151. if(player.getName().equalsIgnoreCase(name)){
    152. if(levels.containsKey(rank)){
    153. int needed = levels.get(rank);
    154. config.set("players." + player.getUniqueId().toString(), config.getInt("players." + player.getUniqueId().toString()) + (needed - config.getInt("players." + player.getUniqueId().toString())));
    155.  
    156. }else{
    157. sender.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "Couldn't find that rank.");
    158. }
    159. config.set("players." + player.getUniqueId().toString(), config.getInt("players." + player.getUniqueId().toString()) - (config.getInt("players." + player.getUniqueId().toString()) - level));
    160. found = true;
    161. break;
    162. }
    163. }
    164. if(found == false){
    165. sender.sendMessage(ChatColor.RED + ChatColor.BOLD.toString() + "Couldn't find player: " + args[1] + "!");
    166. }else{
    167. found = false;
    168. }
    169. return true;
    170. }
    171.  
    172.  
    173.  
    174.  
    175. else if(args[0].equalsIgnoreCase("stats")){
    176. if(!(sender.hasPermission("pvpr.stats"))){
    177. sender.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have permission to do that.");
    178. return true;
    179. }
    180.  
    181. String name = args[1];
    182.  
    183. for(Player player:Bukkit.getOnlinePlayers()){
    184. if(player.getName().equalsIgnoreCase(name)){
    185. sender.sendMessage(ChatColor.GOLD + ChatColor.BOLD.toString() + ChatColor.STRIKETHROUGH + "======================" + ChatColor.GREEN + ChatColor.BOLD.toString() + "[PVPRANKS STATS]" + "======================" +
    186. ChatColor.BOLD + ChatColor.GREEN.toString() + "Player: " + ChatColor.GOLD + ChatColor.ITALIC.toString() + player.getName() +
    187. ChatColor.BOLD + ChatColor.GREEN.toString() + "Rank: " + ChatColor.GOLD + ChatColor.ITALIC.toString() + player.getName() +
    188. ChatColor.BOLD + ChatColor.GREEN.toString() + "Suffix: " + ChatColor.GOLD + ChatColor.ITALIC.toString() + player.getName() +
    189. ChatColor.BOLD + ChatColor.GREEN.toString() + "Points: " + ChatColor.GOLD + ChatColor.ITALIC.toString() + player.getName() +
    190. ChatColor.BOLD + ChatColor.GREEN.toString() + "Points to next level: " + ChatColor.GOLD + ChatColor.ITALIC.toString() + player.getName() +
    191. ChatColor.BOLD + ChatColor.GREEN.toString() + "KDR: " + ChatColor.GOLD + ChatColor.ITALIC.toString() + player.getName()
    192.  
    193. //TODO:
    194. //ADD IN ACTUAL VALUES
    195. //TRACK KILLS
    196. //KILL FARMING PREVENTION
    197.  
    198.  
    199.  
    200.  
    201.  
    202. );
    203.  
    204. found = true;
    205. break;
    206. }
    207. }if(found == false){
    208. sender.sendMessage(ChatColor.RED + ChatColor.BOLD.toString() + "Couldn't find player: " + args[1] + "!");
    209. }else{
    210. found = false;
    211. }
    212. return true;
    213.  
    214. }
    215.  
    216.  
    217.  
    218.  
    219.  
    220.  
    221.  
    222.  
    223. }
    224.  
    225. return true;
    226. }
    227.  
    228.  
    229.  
    230.  
    231. }
    232.  


    And the second:

    Code:java
    1. package me.Caprei.PvPRanks;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Entity;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.PlayerDeathEvent;
    12. import org.bukkit.event.player.AsyncPlayerChatEvent;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14.  
    15. public class PvPListener implements Listener{
    16. PvPRanks plugin;
    17. int experience;
    18. int level;
    19. String suffix;
    20. HashMap<Integer, Integer> list = plugin.levels;
    21. int x = 1;
    22.  
    23. public PvPListener(PvPRanks instance){
    24. plugin = instance;
    25. }
    26.  
    27. @EventHandler
    28. public void onPlayerJoin(PlayerJoinEvent event){
    29. String playeruuid = event.getPlayer().getUniqueId().toString();
    30. if(!(plugin.config.contains("players." + playeruuid))){
    31. plugin.config.addDefault("players." + playeruuid, 0);
    32. experience = 0;
    33. suffix = plugin.config.getString("suffix.0");
    34. suffix = suffix.replaceAll("&", "§");
    35. }else{
    36. experience = plugin.config.getInt("players." + playeruuid);
    37. while(x < 28){
    38. int value = (int) list.get(x);
    39. if(experience < value){
    40. level = x - 1;
    41. break;
    42. }
    43. }
    44. suffix = plugin.config.getString("suffix." + level);
    45. suffix = suffix.replaceAll("&", "§");
    46. }
    47. }
    48.  
    49. @EventHandler
    50. public void onPlayerDeath(PlayerDeathEvent event){
    51. Entity e = event.getEntity();
    52. if(e instanceof Player){
    53. Player p = (Player) e;
    54.  
    55. if(!(p.getKiller() instanceof Player)){
    56. return;
    57. }
    58.  
    59.  
    60. String playeruuid = p.getUniqueId().toString();
    61. int preexperience = plugin.config.getInt("players." + playeruuid);
    62. if(preexperience == 0){
    63. p.sendMessage(ChatColor.GREEN + "You didn't lose any points because you're already on zero!");
    64. return;
    65. }
    66. plugin.config.set("players." + playeruuid, new Integer(experience - plugin.config.getInt("How many points do you lose when you die")));
    67. int afterexperience = plugin.config.getInt("players." + playeruuid);
    68. while(x < 28){
    69. int value = (int) list.get(x);
    70. if(preexperience >= value && afterexperience < value){
    71. level = x;
    72. p.sendMessage(ChatColor.RED + ChatColor.BOLD.toString() + "You went down 1 rank after being killed by " + ChatColor.YELLOW + "[" + ChatColor.GRAY + p.getKiller().getName() + ChatColor.YELLOW + "]");
    73. break;
    74. }
    75. }
    76.  
    77. Player killer = p.getKiller();
    78. p.sendMessage(ChatColor.GREEN + "You lost: " + ChatColor.GOLD + plugin.config.getInt("How many points do you lose when you die") + ChatColor.GREEN + ", for being killed by: " + ChatColor.GOLD + killer.getName());
    79. killer.sendMessage(ChatColor.GREEN + "You got: " + ChatColor.GOLD + plugin.config.getInt("How many points are awarded when you kill someone else") + ChatColor.GREEN + ", for killing: " + ChatColor.GOLD + p.getName());
    80. String killeruuid = killer.getUniqueId().toString();
    81. int killerpreexperience = plugin.config.getInt("players." + killeruuid);
    82. plugin.config.set("players." + killeruuid, new Integer(experience - plugin.config.getInt("How many points do you lose when you die")));
    83. int killerafterexperience = plugin.config.getInt("players." + killeruuid);
    84. while(x < 28){
    85. int value = (int) list.get(x);
    86. if(killerpreexperience < value && killerafterexperience >= value){
    87. level = x;
    88. p.sendMessage(ChatColor.GREEN + ChatColor.BOLD.toString() + "You went up 1 rank after killing " + ChatColor.YELLOW + "[" + ChatColor.GRAY + p.getName() + ChatColor.YELLOW + "]");
    89. break;
    90. }
    91. }
    92.  
    93. }
    94.  
    95.  
    96. }
    97.  
    98.  
    99. @EventHandler
    100. public void onAsyncPlayerChat(AsyncPlayerChatEvent event){
    101. String firstmessage = event.getMessage();
    102. String playerdisplayname = event.getPlayer().getDisplayName();
    103. String finalmessage = playerdisplayname + suffix + ": " + firstmessage;
    104. Bukkit.getServer().broadcastMessage(finalmessage);
    105. }
    106.  
    107. }
    108.  


    Thank you very much for your help!
     
  2. Offline

    fireblast709

    Caprei plugin is null when you try to get plugin.levels. initialise list after you set plugin in the constructor.

    Also:
    • Don't call getConfig() before the plugin is initialised (this happens in the constructor of JavaPlugin).
    • flag == false is the same as !flag. flag == true is the same as flag.
    • Your while loops are infinite since x never increases in value.
    • Don't use the Minecraft Logger (getServer().getLogger()), use the getLogger() from JavaPlugin.
     
    Caprei likes this.
  3. Offline

    Caprei

    fireblast709

    Thanks for your advice. I was really tired when writing this and prone to mistakes like
    • Your while loops are infinite since x never increases in value.
    Thank you so much. Fixing everything. :)
     
Thread Status:
Not open for further replies.

Share This Page