NullPointerException when loading config.

Discussion in 'Plugin Development' started by LFPlugins, Aug 30, 2014.

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

    LFPlugins

    Hey!
    I'm making a plugin with two files, one config and one tokens.yml, where i save a hashmap.
    I get a NullPointerException at line 65 (
    Code:
    [18:33:03 ERROR]: Error occurred while enabling Tokens v1.0 (Is it up to date?)
    java.lang.NullPointerException
            at se.fillefixsweden.tokens.Main.loadConfiguration(Main.java:65) ~[?:?]
            at se.fillefixsweden.tokens.Main.onEnable(Main.java:42) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[c
    raftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:324) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:404) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.jav
    a:455) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.
    java:389) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.n(MinecraftServer.java:3
    52) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.g(MinecraftServer.java:3
    26) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.a(MinecraftServer.java:2
    82) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
            at net.minecraft.server.v1_7_R4.DedicatedServer.init(DedicatedServer.jav
    a:189) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :436) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    )

    here is the affected part of the code:
    Code:java
    1. File f = new File("plugins/Tokens/tokens.yml");
    2. YamlConfiguration yamlFile = YamlConfiguration.loadConfiguration(f);
    3. FileConfiguration config = getConfig();
    4. private static Main plugin;
    5.  
    6.  
    7. HashMap<String, Long> tokens = new HashMap<String, Long>();
    8.  
    9. public void onEnable(){
    10. loadConfiguration();
    11.  
    12. }
    13.  
    14.  
    15.  
    16. public void onDisable() {
    17. for (Entry<String, Long> entry : tokens.entrySet())
    18. {
    19. yamlFile.set(entry.getKey(), entry.getValue());
    20.  
    21. }
    22.  
    23. try {
    24. yamlFile.save(f);
    25. } catch(IOException e) {
    26. // Handle any IO exception here
    27. e.printStackTrace();
    28. }
    29. }
    30.  
    31.  
    32. public void loadConfiguration(){
    33. for (String key : yamlFile.getConfigurationSection("parentnode").getKeys(false))
    34. {
    35. tokens.put(key, yamlFile.getLong("parentnode." + key));
    36. }
    37.  
    38. String path = "Main.Value";
    39. plugin.getConfig().addDefault(path, 200);
    40. plugin.getConfig().options().copyDefaults(true);
    41. plugin.saveConfig();
    42.  
    43. }

    ,


    line 65 is:
    Code:java
    1. for (String key : yamlFile.getConfigurationSection("parentnode").getKeys(false))
    2.  



    Thank you in advance!
     
  2. Offline

    techboy291

    Can we see tokens.yml please?
     
  3. Offline

    LFPlugins

    techboy291 well that should be created at enabling, did i do something wrong with that?

    techboy291 are you there?

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

    techboy291

    LFPlugins I don't see you create parentnode anywhere, you just tried to get the values from it.
     
  5. Offline

    LFPlugins

    techboy291 oh sry, im kind of a noob in configs / yaml files, how do i create it?

    Someone?

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

    techboy291

    LFPlugins Just add a parentnode section in your tokens.yml. If you don't know how to edit yml files, this should help.
     
  7. Offline

    LFPlugins

    I created a file (tokens.yml) with this in it:
    Code:
    tokens:
    i still get the error.

    techboy291 Oops forgot to tahg you

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

    techboy291

    LFPlugins It will still return null if the tokens configuration section is blank. And you replaced
    Code:
    for (String key : yamlFile.getConfigurationSection("parentnode").getKeys(false))
    
    with

    Code:
    for (String key : yamlFile.getConfigurationSection("tokens").getKeys(false))
    
    right?
     
  9. Offline

    LFPlugins

    techboy291 yup i did change it, i cant seem to understand how i should add something too it so it doesnt return as null.
     
  10. Offline

    LFPlugins

    Someone who can help me?
     
  11. Offline

    Epicballzy

    You aren't checking if the file exists. Nor are you creating a new file.
     
  12. Offline

    LFPlugins

    Epicballzy Okey, how do i do that? (both) sorry if i am doing things i really dont understand
     
  13. Offline

    Epicballzy

    Code:java
    1. public void files() {//Would go in onEnable()
    2. File file = new File("config.yml");//Your file
    3. if(file.exists())return;//If the file exists, return
    4. file.createNewFile();//Create new file if it doesn't exist
    5. }
     
  14. Offline

    LFPlugins

  15. Offline

    Epicballzy

    When you put "tokens:" in your config, it still returned null because you're looking for "parentnode".

    Code:java
    1. for(String key : yamlFile.getConfigurationSection("parentnode").getKeys(false))
     
  16. Offline

    LFPlugins

  17. Offline

    techboy291

  18. Offline

    LFPlugins

    techboy291
    Code:
    tokens:
    techboy291 is that right?

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

    techboy291

    LFPlugins Please stop double posting. What did you think getConfigurationSection("tokens") would return? If the configuration section is empty (which it is), calling that method will return null.
     
  20. Offline

    LFPlugins

    techboy291 Okey sorry, i wont. I do not have anything to place under tokens becuase it will generate it by itself later on...
     
Thread Status:
Not open for further replies.

Share This Page