NullPointerException on Config Calling

Discussion in 'Plugin Development' started by ColaCraft, Aug 10, 2013.

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

    ColaCraft

    Heh, yet another null pointer. Here is the error & the bits of code you'll need to help me work this out.

    Error:

    [​IMG]

    I found out that this error is being caused by the way I call my config. I used this tutorial earlier today, after working out some bugs in it, I get this. Here is the tutorial:

    http://forums.bukkit.org/threads/tut-custom-yaml-configurations-with-comments.142592/

    My code is the exact same as that almost, I just renamed the classes ConfigA, & ConfigB.

    Here is all of the code that deals with the config:

    Config A:
    Code:java
    1.  
    2. package me.PorterK.RPG;
    3.  
    4. import java.io.BufferedReader;
    5. import java.io.BufferedWriter;
    6. import java.io.ByteArrayInputStream;
    7. import java.io.File;
    8. import java.io.FileOutputStream;
    9. import java.io.FileReader;
    10. import java.io.FileWriter;
    11. import java.io.IOException;
    12. import java.io.InputStream;
    13. import java.io.OutputStream;
    14. import java.nio.charset.Charset;
    15.  
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class ConfigA {
    19.  
    20. private JavaPlugin plugin;
    21.  
    22. /*
    23.   * Manage custom configurations and files
    24.   */
    25. public ConfigA(JavaPlugin plugin) {
    26. this.plugin = plugin;
    27. }
    28.  
    29. /*
    30.   * Get new configuration with header
    31.   * @param filePath - Path to file
    32.   * @return - New ConfigB
    33.   */
    34. public ConfigB getNewConfig(String filePath, String[] header) {
    35.  
    36. File file = this.getConfigFile(filePath);
    37.  
    38. if(!file.exists()) {
    39. this.prepareFile(filePath);
    40.  
    41. if(header != null && header.length != 0) {
    42. this.setHeader(file, header);
    43. }
    44.  
    45. }
    46.  
    47. ConfigB config = new ConfigB(this.getConfigContent(filePath), file, this.getCommentsNum(file), plugin);
    48. return config;
    49.  
    50. }
    51.  
    52. /*
    53.   * Get new configuration
    54.   * @param filePath - Path to file
    55.   * @return - New ConfigB
    56.   */
    57. public ConfigB getNewConfig(String filePath) {
    58. return this.getNewConfig(filePath, null);
    59. }
    60.  
    61. /*
    62.   * Get configuration file from string
    63.   * @param file - File path
    64.   * @return - New file object
    65.   */
    66. private File getConfigFile(String file) {
    67.  
    68. if(file.isEmpty() || file == null) {
    69. return null;
    70. }
    71.  
    72. File configFile;
    73.  
    74. if(file.contains("/")) {
    75.  
    76. if(file.startsWith("/")) {
    77. configFile = new File(plugin.getDataFolder() + file.replace("/", File.separator));
    78. } else {
    79. configFile = new File(plugin.getDataFolder() + File.separator + file.replace("/", File.separator));
    80. }
    81.  
    82. } else {
    83. configFile = new File(plugin.getDataFolder(), file);
    84. }
    85.  
    86. return configFile;
    87.  
    88. }
    89.  
    90. /*
    91.   * Create new file for config and copy resource into it
    92.   * @param file - Path to file
    93.   * @param resource - Resource to copy
    94.   */
    95. public void prepareFile(String filePath, String resource) {
    96.  
    97. File file = this.getConfigFile(filePath);
    98.  
    99. if(file.exists()) {
    100. return;
    101. }
    102.  
    103. try {
    104. file.getParentFile().mkdirs();
    105. file.createNewFile();
    106. if(resource != null && !resource.isEmpty()) {
    107. this.copyResource(plugin.getResource(resource), file);
    108. }
    109.  
    110. } catch (IOException e) {
    111. e.printStackTrace();
    112. }
    113.  
    114. }
    115.  
    116. /*
    117.   * Create new file for config without resource
    118.   * @param file - File to create
    119.   */
    120. public void prepareFile(String filePath) {
    121. this.prepareFile(filePath, null);
    122. }
    123.  
    124. /*
    125.   * Adds header block to config
    126.   * @param file - Config file
    127.   * @param header - Header lines
    128.   */
    129. public void setHeader(File file, String[] header) {
    130.  
    131. if(!file.exists()) {
    132. return;
    133. }
    134.  
    135. try {
    136. String currentLine;
    137. StringBuilder config = new StringBuilder("");
    138. BufferedReader reader = new BufferedReader(new FileReader(file));
    139.  
    140. while((currentLine = reader.readLine()) != null) {
    141. config.append(currentLine + "\n");
    142. }
    143.  
    144. reader.close();
    145. config.append("# +----------------------------------------------------+ #\n");
    146.  
    147. for(String line : header) {
    148.  
    149. if(line.length() > 50) {
    150. continue;
    151. }
    152.  
    153. int lenght = (50 - line.length()) / 2;
    154. StringBuilder finalLine = new StringBuilder(line);
    155.  
    156. for(int i = 0; i < lenght; i++) {
    157. finalLine.append(" ");
    158. finalLine.reverse();
    159. finalLine.append(" ");
    160. finalLine.reverse();
    161. }
    162.  
    163. if(line.length() % 2 != 0) {
    164. finalLine.append(" ");
    165. }
    166.  
    167. config.append("# < " + finalLine.toString() + " > #\n");
    168.  
    169. }
    170.  
    171. config.append("# +----------------------------------------------------+ #");
    172.  
    173. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
    174. writer.write(this.prepareConfigString(config.toString()));
    175. writer.flush();
    176. writer.close();
    177.  
    178. } catch (IOException e) {
    179. e.printStackTrace();
    180. }
    181.  
    182. }
    183.  
    184. /*
    185.   * Read file and make comments SnakeYAML friendly
    186.   * @param filePath - Path to file
    187.   * @return - File as Input Stream
    188.   */
    189. public InputStream getConfigContent(File file) {
    190.  
    191. if(!file.exists()) {
    192. return null;
    193. }
    194.  
    195. try {
    196. int commentNum = 0;
    197.  
    198. String addLine;
    199. String currentLine;
    200. String pluginName = this.getPluginName();
    201.  
    202. StringBuilder whole = new StringBuilder("");
    203. BufferedReader reader = new BufferedReader(new FileReader(file));
    204.  
    205. while((currentLine = reader.readLine()) != null) {
    206.  
    207. if(currentLine.startsWith("#")) {
    208. addLine = currentLine.replaceFirst("#", pluginName + "_COMMENT_" + commentNum + ":");
    209. whole.append(addLine + "\n");
    210. commentNum++;
    211.  
    212. } else {
    213. whole.append(currentLine + "\n");
    214. }
    215.  
    216. }
    217.  
    218. String config = whole.toString();
    219. InputStream configStream = new ByteArrayInputStream(config.getBytes(Charset.forName("UTF-8")));
    220.  
    221. reader.close();
    222. return configStream;
    223.  
    224. } catch (IOException e) {
    225. e.printStackTrace();
    226. return null;
    227. }
    228.  
    229. }
    230.  
    231. /*
    232.   * Get comments from file
    233.   * @param file - File
    234.   * @return - Comments number
    235.   */
    236. private int getCommentsNum(File file) {
    237.  
    238. if(!file.exists()) {
    239. return 0;
    240. }
    241.  
    242. try {
    243. int comments = 0;
    244. String currentLine;
    245.  
    246. BufferedReader reader = new BufferedReader(new FileReader(file));
    247.  
    248. while((currentLine = reader.readLine()) != null) {
    249.  
    250. if(currentLine.startsWith("#")) {
    251. comments++;
    252. }
    253.  
    254. }
    255.  
    256. reader.close();
    257. return comments;
    258.  
    259. } catch (IOException e) {
    260. e.printStackTrace();
    261. return 0;
    262. }
    263.  
    264. }
    265.  
    266. /*
    267.   * Get config content from file
    268.   * @param filePath - Path to file
    269.   * @return - readied file
    270.   */
    271. public InputStream getConfigContent(String filePath) {
    272. return this.getConfigContent(this.getConfigFile(filePath));
    273. }
    274.  
    275.  
    276. private String prepareConfigString(String configString) {
    277.  
    278. int lastLine = 0;
    279. int headerLine = 0;
    280.  
    281. String[] lines = configString.split("\n");
    282. StringBuilder config = new StringBuilder("");
    283.  
    284. for(String line : lines) {
    285.  
    286. if(line.startsWith(this.getPluginName() + "_COMMENT")) {
    287. String comment = "#" + line.trim().substring(line.indexOf(":") + 1);
    288.  
    289. if(comment.startsWith("# +-")) {
    290.  
    291. /*
    292.   * If header line = 0 then it is
    293.   * header start, if it's equal
    294.   * to 1 it's the end of header
    295.   */
    296.  
    297. if(headerLine == 0) {
    298. config.append(comment + "\n");
    299.  
    300. lastLine = 0;
    301. headerLine = 1;
    302.  
    303. } else if(headerLine == 1) {
    304. config.append(comment + "\n\n");
    305.  
    306. lastLine = 0;
    307. headerLine = 0;
    308.  
    309. }
    310.  
    311. } else {
    312.  
    313. /*
    314.   * Last line = 0 - Comment
    315.   * Last line = 1 - Normal path
    316.   */
    317.  
    318. String normalComment;
    319.  
    320. if(comment.startsWith("# ' ")) {
    321. normalComment = comment.substring(0, comment.length() - 1).replaceFirst("# ' ", "# ");
    322. } else {
    323. normalComment = comment;
    324. }
    325.  
    326. if(lastLine == 0) {
    327. config.append(normalComment + "\n");
    328. } else if(lastLine == 1) {
    329. config.append("\n" + normalComment + "\n");
    330. }
    331.  
    332. lastLine = 0;
    333.  
    334. }
    335.  
    336. } else {
    337. config.append(line + "\n");
    338. lastLine = 1;
    339. }
    340.  
    341. }
    342.  
    343. return config.toString();
    344.  
    345. }
    346.  
    347.  
    348. /*
    349.   * Saves configuration to file
    350.   * @param configString - Config string
    351.   * @param file - Config file
    352.   */
    353. public void saveConfig(String configString, File file) {
    354. String configuration = this.prepareConfigString(configString);
    355.  
    356. try {
    357. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
    358. writer.write(configuration);
    359. writer.flush();
    360. writer.close();
    361.  
    362. } catch (IOException e) {
    363. e.printStackTrace();
    364. }
    365.  
    366. }
    367.  
    368. public String getPluginName() {
    369. return plugin.getDescription().getName();
    370. }
    371.  
    372. /*
    373.   * Copy resource from Input Stream to file
    374.   * @param resource - Resource from .jar
    375.   * @param file - File to write
    376.   */
    377. private void copyResource(InputStream resource, File file) {
    378.  
    379. try {
    380. OutputStream out = new FileOutputStream(file);
    381.  
    382. int lenght;
    383. byte[] buf = new byte[1024];
    384.  
    385. while((lenght = resource.read(buf)) > 0){
    386. out.write(buf, 0, lenght);
    387. }
    388.  
    389. out.close();
    390. resource.close();
    391.  
    392. } catch (Exception e) {
    393. e.printStackTrace();
    394. }
    395.  
    396. }
    397.  
    398. }
    399.  


    Config B:

    Code:java
    1. package me.PorterK.RPG;
    2.  
    3. /**
    4. * @author Mike
    5. *
    6. */
    7. import java.io.File;
    8. import java.io.InputStream;
    9. import java.util.List;
    10. import java.util.Set;
    11.  
    12. import org.bukkit.configuration.ConfigurationSection;
    13. import org.bukkit.configuration.file.FileConfiguration;
    14. import org.bukkit.configuration.file.YamlConfiguration;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class ConfigB {
    18. private int comments;
    19. private ConfigA manager;
    20.  
    21. private File file;
    22. private FileConfiguration config;
    23.  
    24. public ConfigB(InputStream configStream, File configFile, int comments, JavaPlugin plugin) {
    25. this.comments = comments;
    26. this.manager = new ConfigA(plugin);
    27.  
    28. this.file = configFile;
    29. this.config = YamlConfiguration.loadConfiguration(configStream);
    30. }
    31.  
    32. public Object get(String path) {
    33. return this.config.get(path);
    34. }
    35.  
    36. public Object get(String path, Object def) {
    37. return this.config.get(path, def);
    38. }
    39.  
    40. public String getString(String path) {
    41. return this.config.getString(path);
    42. }
    43.  
    44. public String getString(String path, String def) {
    45. return this.config.getString(path, def);
    46. }
    47.  
    48. public int getInt(String path) {
    49. return this.config.getInt(path);
    50. }
    51.  
    52. public int getInt(String path, int def) {
    53. return this.config.getInt(path, def);
    54. }
    55.  
    56. public boolean getBoolean(String path) {
    57. return this.config.getBoolean(path);
    58. }
    59.  
    60. public boolean getBoolean(String path, boolean def) {
    61. return this.config.getBoolean(path, def);
    62. }
    63.  
    64. public void createSection(String path) {
    65. this.config.createSection(path);
    66. }
    67.  
    68. public ConfigurationSection getConfigurationSection(String path) {
    69. return this.config.getConfigurationSection(path);
    70. }
    71.  
    72. public double getDouble(String path) {
    73. return this.config.getDouble(path);
    74. }
    75.  
    76. public double getDouble(String path, double def) {
    77. return this.config.getDouble(path, def);
    78. }
    79.  
    80. public List<?> getList(String path) {
    81. return this.config.getList(path);
    82. }
    83.  
    84. public List<?> getList(String path, List<?> def) {
    85. return this.config.getList(path, def);
    86. }
    87.  
    88. public boolean contains(String path) {
    89. return this.config.contains(path);
    90. }
    91.  
    92. public void removeKey(String path) {
    93. this.config.set(path, null);
    94. }
    95.  
    96. public void set(String path, Object value) {
    97. this.config.set(path, value);
    98. }
    99.  
    100. public void set(String path, Object value, String comment) {
    101. if(!this.config.contains(path)) {
    102. this.config.set(manager.getPluginName() + "_COMMENT_" + comments, " " + comment);
    103. comments++;
    104. }
    105.  
    106. this.config.set(path, value);
    107.  
    108. }
    109.  
    110. public void set(String path, Object value, String[] comment) {
    111.  
    112. for(String comm : comment) {
    113.  
    114. if(!this.config.contains(path)) {
    115. this.config.set(manager.getPluginName() + "_COMMENT_" + comments, " " + comm);
    116. comments++;
    117. }
    118.  
    119. }
    120.  
    121. this.config.set(path, value);
    122.  
    123. }
    124.  
    125. public void setHeader(String[] header) {
    126. manager.setHeader(this.file, header);
    127. this.comments = header.length + 2;
    128. this.reloadConfig();
    129. }
    130.  
    131. public void reloadConfig() {
    132. this.config = YamlConfiguration.loadConfiguration(manager.getConfigContent(file));
    133. }
    134.  
    135. public void saveConfig() {
    136. String config = this.config.saveToString();
    137. manager.saveConfig(config, this.file);
    138.  
    139. }
    140.  
    141. public Set<String> getKeys() {
    142. return this.config.getKeys(false);
    143. }
    144.  
    145. }


    Here is where I deal with the code in the main class (Inside the onEnable()):

    Code:java
    1. String[] comments = {"Here is where your", "users will be", "stored."};
    2. String[] header = {"ColaRPG", "Beta v1.0", "AutoUpdating is enabled by default!"};
    3.  
    4. this.manager = new ConfigA(this);
    5.  
    6. this.config = manager.getNewConfig("config.yml", header);
    7. this.messages = manager.getNewConfig("sql/setup.yml");
    8.  
    9. this.config.set("AutoUpdatingEnabled", "true", "Enable(true) / disable(false) auto updating.");
    10.  
    11. this.config.set("users", "", comments);
    12. this.config.saveConfig();


    Here is where the error can be found:

    Code:java
    1. if(plugin.getConfig().getString("users." + name + ".class").equalsIgnoreCase("Wizard")){


    Last but not least, here is where it adds you to the "wizard" group via command.

    Code:java
    1. if(args[1] != null && (args[1].equalsIgnoreCase("wizard"))){
    2.  
    3. if(plugin.getConfig().getString("users." + name + ".class") != "Wizard"){
    4.  
    5. plugin.getConfig().set("users." + name + ".class", "Wizard");
    6. p.getInventory().addItem(CustomItems.WizardsWand);
    7. sender.sendMessage(ChatColor.GREEN + "You are now a " + ChatColor.DARK_RED + "wizard" + ChatColor.GREEN + "!");
    8. p.setMaxHealth(18);
    9. p.setHealth(18);
    10. plugin.saveConfig();
    11.  
    12. }else if(plugin.getConfig().getString("users." + name + ".class") == "Wizard"){
    13.  
    14. sender.sendMessage(ChatColor.RED + "You are already a " + ChatColor.DARK_RED + "Wizard" + ChatColor.RED + "!");
    15.  
    16. }
    17.  
    18. }


    I suspect the issue to be in how I call upon the config, as it is not the same as how I create the default config.

    Notably, when I add myself to the wizard class, it deletes the comments I created with the SimpleConfig thing (I posted the link above, it's the tutorial)
     
  2. Offline

    newboyhun

    ColaCraft
    You gave me us a bunch of code but we don't know the line with the error.
    Cheer =D
     
  3. Offline

    ColaCraft


    The line with the error is line 84, as seen in the console picture. That line is marked with a comment, but I will go ahead & post it separately, I suppose.

    Code:java
    1. if (plugin.cooldown1.get(p.getName()) <= 0) {


    newboyhun
     
  4. Offline

    iFamasssxD

    They are obviously not in the hashMap instead of checking for a null statement in the hashmap check it like this.
    Code:
    if(!plugin.cooldown.containsKey(player.getName()){
    plugin.cooldown.put(player.getName(), 0);
    }
    
    Run a check to see if they are in the hashmap before you get the numbers from it.
     
  5. Offline

    ColaCraft

    Hmm... odd.

    Here is the new error:

    [​IMG]

    It now says the error is on line 80, which happens to be here:

    Code:java
    1. if(plugin.getConfig().getString("users." + name + ".class").equalsIgnoreCase("Wizard")){


    That would make sense, as I just added in a new config handler- I don't see why it would hinder getting things from the configs though. I have commands set up to change your class (It changes it in the config) & that works fine. Hmm...

    iFamasssxD
    newboyhun

    Updated OP- the issue is not the same as I thought.

    Log-out
    Trevor1134

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

    newboyhun

Thread Status:
Not open for further replies.

Share This Page