Is it me or I can't use FileConfiguration?

Discussion in 'Plugin Development' started by Jaker232, Jan 7, 2014.

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

    Jaker232

    I've been planning to rewrite all my code for my Work-In-Progress plugin because, the code is messy and terrible. I nailed down the Configuration class and I have it the way I want it to be right now. I also written parts of the code, code that was giving me trouble.
    It's the same code giving me problems.
    For example, here's the stacktrace.

    Stacktrace (open)

    [Server thread/ERROR]: Error occurred while enabling plugin v0.1 (Is it up to date?)
    java.lang.NullPointerException
    at code.Jaker232.xxx.xxx.onEnable(xxx.java:18) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:218) ~[craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:384) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugin(CraftServer.java:298) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.enablePlugins(CraftServer.java:280) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.m(MinecraftServer.java:342) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.g(MinecraftServer.java:319) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.a(MinecraftServer.java:275) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:175) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:424) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit-1.7.2-R0.2.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]



    I read the stacktrace and found it was a NullPointerException problem, in my main class on line 18.
    Code:java
    1.  
    2. if(config.getValueOfKey("database.type").equals("mysql")) {
    3.  



    This is my line 18. Config points to my configuration class which is:
    Code:java
    1. package code.Jaker232.xxx;
    2. import java.io.File;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. import java.util.logging.Level;
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8. public final class Configuration {
    9.  
    10. private FileConfiguration config;
    11. private File configFile;
    12. private xxx plugin;
    13.  
    14. private String path;
    15.  
    16. private boolean didFileExistBeforeCode;
    17.  
    18. public Configuration() {
    19. plugin = new xxx();
    20. path = (plugin.getDataFolder() + File.separator + "plugins" + File.separator + "xxx");
    21. configFile = new File(path, "config.yml");
    22. if(!configFile.exists()){
    23. didFileExistBeforeCode = false;
    24. configFile.mkdirs();
    25. try {
    26. configFile.createNewFile();
    27. } catch (IOException e) {
    28. e.printStackTrace();
    29. }
    30. } else {
    31. didFileExistBeforeCode = true;
    32. }
    33. config = YamlConfiguration.loadConfiguration(configFile);
    34. if(!didFileExistBeforeCode) {
    35.  
    36. }
    37. }
    38.  
    39. public void reloadCustomConfig() {
    40. if (configFile == null) {
    41. configFile = new File(path, "config.yml");
    42. }
    43. config = YamlConfiguration.loadConfiguration(configFile);
    44.  
    45. // Look for defaults in the jar
    46. InputStream defConfigStream = plugin.getResource("customConfig.yml");
    47. if (defConfigStream != null) {
    48. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    49. config.setDefaults(defConfig);
    50. }
    51. }
    52.  
    53.  
    54. public void saveCustomConfig() {
    55. if (config == null || configFile == null) {
    56. return;
    57. }
    58. try {
    59. config.save(configFile);
    60. } catch (IOException ex) {
    61. plugin.getLogger().log(Level.SEVERE, "Could not save config to " + configFile, ex);
    62. }
    63. }
    64.  
    65. public FileConfiguration getCustomConfig() {
    66. if (config == null) {
    67. reloadCustomConfig();
    68. }
    69. return config;
    70. }
    71.  
    72. public Object getValueOfKey(String key) {
    73. return config.get(key);
    74. }
    75. }

    I don't know what's wrong with my code there. Do any of you have an idea why this is happened?
     
  2. your config is null, place the folowing code at the top inside your getValueOfKey
    Code:java
    1. if (config == null) {
    2. reloadCustomConfig();
    3. }
     
  3. Offline

    Jaker232

    ferrybig The issue persists.
    I think it's the config.yml not appearing. I thought I had the code to make the file... do you know the proper way to make directories and files?
     
  4. Jaker232 You can't just create a new instance of your plugin, all the internal values won't be populated.

    Also, do you really value the name of your plugin soo high that you hide it? Wow.
     
  5. Offline

    Jaker232

    How would I send logger info, then?
    I'm going to import Logger and use that.

    kumpelblase2 Actually, I need the main class to be able to access getDataFolder() AND getLogger(). Any alternatives?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  6. I don't know whats so hard about just referencing the main instance in your configuration class.
     
  7. Offline

    Jaker232

    "You can't just create a new instance of your plugin" I don't know how else to access the class without making a new instance.
     
  8. I know, that's why you pass over a reference to your configuration class! Haven't you learned this yet?
     
  9. Offline

    Jaker232

    Apparently your wording confuses the living crap out of me.
     
  10. Offline

    Jaker232

    kumpelblase2 So I should put xxx xx in the constructor as a variable then do plugin = xx;, thanks for clarifying.
     
Thread Status:
Not open for further replies.

Share This Page