NPE can't find problem

Discussion in 'Plugin Development' started by Gopaintman, Sep 7, 2013.

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

    Gopaintman

    Hi,
    The command lb kills <playername> <num> returns a null pointer exception at line 78 in the main class.

    Main Class:
    Code:java
    1. package com.katsaroucraft.gopaintman;
    2.  
    3.  
    4.  
    5.  
    6. import java.io.File;
    7. import java.io.IOException;
    8. import java.io.InputStream;
    9. import java.util.logging.Level;
    10.  
    11. import org.bukkit.Bukkit;
    12. import org.bukkit.ChatColor;
    13. import org.bukkit.command.Command;
    14. import org.bukkit.command.CommandSender;
    15. import org.bukkit.configuration.file.FileConfiguration;
    16. import org.bukkit.configuration.file.YamlConfiguration;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.Event;
    19. import org.bukkit.event.EventHandler;
    20. import org.bukkit.event.EventPriority;
    21. import org.bukkit.event.Listener;
    22. import org.bukkit.event.player.PlayerJoinEvent;
    23. import org.bukkit.plugin.PluginManager;
    24. import org.bukkit.plugin.java.JavaPlugin;
    25.  
    26.  
    27.  
    28.  
    29.  
    30. public class Main extends JavaPlugin implements Listener{
    31. static File statsFile = null;
    32. static FileConfiguration statsFileConfig = null;
    33. static FileUtil file;
    34. String[] help = {"help: Displays help info", "setsign <stat>: removes a selected leaderboard", "delsign"};
    35. String prefix;
    36.  
    37.  
    38. @Override
    39. public void onEnable() {
    40. PluginManager pm = this.getServer().getPluginManager();
    41. pm.registerEvents(new FileUtil(this), this);
    42. this.getConfig().options().copyDefaults(true);
    43. reloadStatsFile();
    44. this.saveDefaultConfig();
    45. this.saveDefaultStatsFile();
    46. this.prefix = ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("prefix"));
    47. getServer().getLogger();
    48.  
    49.  
    50. }
    51.  
    52.  
    53.  
    54. @Override
    55. public void onDisable() {
    56. getServer().getLogger();
    57. this.saveDefaultConfig();
    58.  
    59. }
    60. public boolean onCommand(CommandSender sender, Command cmd,String commandLabel, String[] args) {
    61. if(cmd.getName().equalsIgnoreCase("lb")){
    62. if(args.length>0){
    63. if(args[0].equalsIgnoreCase("help")){
    64. sender.sendMessage("");
    65. return false;
    66. }else if(args[0].equalsIgnoreCase("setsign")){
    67. //get WE API
    68. //sadface
    69. }else if(args[0].equalsIgnoreCase("delsign")){
    70. //get WE API
    71. //sadface
    72. }else if(args[0].equalsIgnoreCase("reset")){
    73. //reset dem stats you
    74. }else if(args[0].equalsIgnoreCase("kills")){
    75. if(args.length > 2){
    76. if(getPlayer(args[1]) != null){
    77. String p = getPlayer(args[1]).getName();
    78. if(file.fileExists(p)){
    79. int kills = Integer.parseInt(args[2]);
    80. file.setKills(p, kills);
    81. sender.sendMessage(p + "'s kills have been set to " + kills);
    82. }else{
    83. sender.sendMessage(prefix + "Player file not found.");
    84.  
    85. }
    86. }else{
    87. sender.sendMessage(prefix + "Player not found.");
    88.  
    89. }
    90. }else{
    91. sender.sendMessage("Not enough args!");
    92. sender.sendMessage(ChatColor.RED + "");
    93. }
    94. }else if(args[0].equalsIgnoreCase("deaths")){
    95. if(args.length > 2){
    96. if(getPlayer(args[1]) != null){
    97. String p = getPlayer(args[1]).getName();
    98. if(file.fileExists(p)){
    99. int deaths = Integer.parseInt(args[2]);
    100. file.setDeaths(p, deaths);
    101. sender.sendMessage(p + "'s deaths have been set to " + deaths);
    102. }else{
    103. sender.sendMessage(prefix + "Player file not found.");
    104.  
    105. }
    106. }else{
    107. sender.sendMessage(prefix + "Player not found.");
    108.  
    109. }
    110. }else{
    111. sender.sendMessage("Not enough args!");
    112. }
    113. }else if(args[0].equalsIgnoreCase("kdr")){
    114. if(sender instanceof Player){
    115. final Player player = (Player) sender;
    116. String name = player.getName();
    117. int kills = file.getKills(name);
    118. int deaths = file.getKills(name);
    119. if(deaths != 0){
    120. double kdr = kills/deaths;
    121. sender.sendMessage(prefix + "Your Kill to Death ratio is " + kdr);
    122. }else{
    123. sender.sendMessage(prefix + "You have no deaths!");
    124. }
    125.  
    126. }else{
    127. sender.sendMessage("This command can only be sent by a player");
    128. }
    129. }else if(args[0].equalsIgnoreCase("reload")){
    130. try {
    131. this.reloadConfig();
    132. this.reloadStatsFile();
    133. sender.sendMessage("InGameLeaderboard files have reloaded succesfully!");
    134. getLogger().info("InGameLeaderboard files have reloaded succesfully!");
    135. getLogger().info("Remeber that player files dont' reload.");
    136. }catch(Exception e){
    137. getLogger().log(Level.SEVERE, "InGameLeaderboard files failed to reload!");
    138. sender.sendMessage("InGameLeaderboard files failed to reload!");
    139. e.printStackTrace();
    140. }
    141. }else{
    142. //put help in here
    143. }
    144.  
    145. }else{
    146. sender.sendMessage("");
    147. return false;
    148. }
    149. }
    150. return true;
    151.  
    152.  
    153.  
    154. }
    155. public void reloadStatsFile(){
    156. if(statsFile == null){
    157. statsFile = new File(getDataFolder(), "stats.yml");
    158. }
    159. statsFileConfig = YamlConfiguration.loadConfiguration(statsFile);
    160. //Look for defaults in the jar
    161. InputStream defConfigStream = this.getResource("stats.yml");
    162. if(defConfigStream != null){
    163. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    164. statsFileConfig.setDefaults(defConfig);
    165. }
    166. }
    167. public FileConfiguration getStatsFile(){
    168. if(statsFileConfig == null){
    169. reloadStatsFile();
    170. }
    171. return statsFileConfig;
    172. }
    173. public void saveStatsFile(){
    174. if(statsFile == null || statsFileConfig == null){
    175. return;
    176. }
    177. try{
    178. getStatsFile().save(statsFile);
    179. }catch(IOException ex){
    180. getLogger().log(Level.SEVERE, "Could not save config " + statsFile, ex);
    181. }
    182.  
    183. }
    184. public void saveDefaultStatsFile() {
    185. if (statsFile == null) {
    186. statsFile = new File(getDataFolder(), "stats.yml");
    187. }
    188. if (!statsFile.exists()) {
    189. this.saveResource("stats.yml", false);
    190. }
    191. }
    192. public Player getPlayer(String name) {
    193. name = name.toLowerCase();
    194. for (Player player : Bukkit.getOnlinePlayers()) {
    195.  
    196. if (player.getName().toLowerCase().contains(name) || player.getDisplayName().toLowerCase().contains(name)) {
    197. return player;
    198. }
    199. }
    200. return null;
    201. }
    202.  
    203.  
    204.  
    205.  
    206.  
    207. /*public void createPlayerPaths(){
    208.   for(int i = 0; i <= Bukkit.getOnlinePlayers().length;i++ ){
    209.   Player[] players = Bukkit.getOnlinePlayers();
    210.   String x = "Players." + players[0];
    211.   StatsFileConfig.ad
    212.  
    213.   }
    214.  
    215.   }*/
    216.  
    217.  
    218. }
    219.  

    Command:
    Code:java
    1. else if(args[0].equalsIgnoreCase("kills")){
    2. if(args.length > 2){
    3. if(getPlayer(args[1]) != null){
    4. String p = getPlayer(args[1]).getName();
    5. if(file.fileExists(p)){
    6. int kills = Integer.parseInt(args[2]);
    7. file.setKills(p, kills);
    8. sender.sendMessage(p + "'s kills have been set to " + kills);
    9. }else{
    10. sender.sendMessage(prefix + "Player file not found.");
    11.  
    12. }
    13. }else{
    14. sender.sendMessage(prefix + "Player not found.");
    15.  
    16. }
    17. }else{
    18. sender.sendMessage("Not enough args!");
    19. sender.sendMessage(ChatColor.RED + "");
    20. }
    21. }


    FileUtil:
    Code:java
    1. package com.katsaroucraft.gopaintman;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.OfflinePlayer;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.EventPriority;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.player.PlayerJoinEvent;
    16.  
    17. public class FileUtil implements Listener {
    18.  
    19. private Main plugin;
    20.  
    21. public FileUtil (Main instance) {
    22. this.plugin = instance;
    23. }
    24.  
    25. public void createFile(Player p) {
    26. File pFileDir = new File(this.plugin.getDataFolder(), "Players");
    27. if (!pFileDir.exists()) {
    28. pFileDir.mkdir();
    29. }
    30. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.getName().toLowerCase() + ".yml");
    31. if (!pFile.exists())
    32. try {
    33. pFile.createNewFile();
    34. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    35. pConfig.set("User", p.getName());
    36. pConfig.set("Kills", Integer.valueOf(0));
    37. pConfig.set("Deaths", Integer.valueOf(0));
    38. pConfig.save(pFile);
    39. }
    40. catch (Exception e) {
    41. }
    42. }
    43.  
    44.  
    45. public String getPlayerName(String p) {
    46. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    47. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    48. String name = pConfig.getString("User");
    49. return name;
    50. }
    51.  
    52. public boolean fileExists(String p) {
    53. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    54. if (pFile.exists()) {
    55. return true;
    56. }else{
    57. return false;
    58. }
    59. }
    60.  
    61. public Integer getKills(String p)
    62. {
    63. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    64. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    65. int kills = pConfig.getInt("Kills");
    66. return Integer.valueOf(kills);
    67. }
    68. public void setKills(String p, int newAmount) {
    69. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    70. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    71. pConfig.set("Kills", Integer.valueOf(newAmount));
    72. try {
    73. pConfig.save(pFile);
    74. } catch (Exception e) {
    75. }
    76. }
    77. public void addKill(String p) {
    78. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    79. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    80. pConfig.set("Kills", Integer.valueOf(pConfig.getInt("Kills") + 1));
    81. try {
    82. pConfig.save(pFile);
    83. } catch (Exception e) {
    84. }
    85. }
    86.  
    87. public Integer getDeaths(String p) {
    88. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    89. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    90. int deaths = pConfig.getInt("Deaths");
    91. return Integer.valueOf(deaths);
    92. }
    93. public void setDeaths(String p, int newAmount) {
    94. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    95. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    96. pConfig.set("Deaths", Integer.valueOf(newAmount));
    97. try {
    98. pConfig.save(pFile);
    99. } catch (Exception e) {
    100. }
    101. }
    102. public void addDeath(String p) {
    103. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    104. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    105. pConfig.set("Deaths", Integer.valueOf(pConfig.getInt("Deaths") + 1));
    106. try {
    107. pConfig.save(pFile);
    108. } catch (Exception e) {
    109. }
    110. }
    111.  
    112. public Integer getPoints(String p) {
    113. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    114. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    115. int Points = pConfig.getInt("Points");
    116. return Integer.valueOf(Points);
    117. }
    118.  
    119. public void setPoints(String p, int newAmount) {
    120. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    121. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    122. pConfig.set("Points", Integer.valueOf(newAmount));
    123. try {
    124. pConfig.save(pFile);
    125. } catch (Exception e) {
    126. }
    127. }
    128.  
    129. public void addPoints(String p, int amountAdded) {
    130. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    131. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    132. pConfig.set("Points", Integer.valueOf(pConfig.getInt("Points") + amountAdded));
    133. try {
    134. pConfig.save(pFile);
    135. } catch (Exception e) {
    136. }
    137. }
    138.  
    139. public void takePoints(String p, int amountTaken) {
    140. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    141. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    142. int PointsCurrent = pConfig.getInt("Points");
    143. if (PointsCurrent - amountTaken >= 0) {
    144. int newAmount = PointsCurrent - amountTaken;
    145. pConfig.set("Points", Integer.valueOf(newAmount));
    146. }
    147. try {
    148. pConfig.save(pFile);
    149. } catch (Exception e) {
    150. }
    151. }
    152. @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    153. public void onPlayerLogin(PlayerJoinEvent e) throws IOException{
    154. final Player p = e.getPlayer();
    155. String name = p.getName();
    156. if(!this.fileExists(p.getName())){
    157. plugin.getLogger().info("found");
    158. e.getPlayer().sendMessage("Creating your data file...");
    159.  
    160. this.createFile(p);
    161. }else{
    162. p.sendMessage("You currently have: "+ this.getKills(name) +" kills, "+ this.getDeaths(name)+ " deaths");
    163. }
    164. }
    165.  
    166. }
     
  2. Offline

    DevRosemberg

    Gopaintman I dont know if it would work with my FileUtil, and please, if you use it say that i made it...
     
  3. Offline

    Gopaintman

    Will do, however what do you mean "I don't know if it would work with my FileUtil"?
     
  4. Offline

    DevRosemberg

    you dont have to do this:

    Code:java
    1. if(file.fileExists(p)){


    the file util alredy does that for you.
     
    Gopaintman likes this.
  5. Offline

    Janmm14

    the problem i think is, that you never set the cariable file to anything.

    Replace
    pm.registerevents(new fileutil(this),this)
    with
    file = new FileUtil(this);
    pm.registerevents(file, this);
     
  6. Offline

    Gopaintman

    Yea it seems that your fileutil always passes a NPE for whenever I try to access it.
     
  7. Offline

    Janmm14

    look at my previous post
     
    Gopaintman likes this.
  8. Offline

    Gopaintman

    Wow I missed that. Thanks for pointing that out to me!
     
Thread Status:
Not open for further replies.

Share This Page