Loading a HashMap saved to file

Discussion in 'Plugin Development' started by dark navi, Jul 16, 2012.

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

    dark navi

    Hey guys, I am having issues reading HashMaps that I save to file.

    Here is an example:

    I have a HashMap<Integer, String>, and this is how it writes to file:
    Code:
    messages:
      75: This is a test message.
    It works fine on the writing end, but I am having issues retrieving the data. Here is what I thought would work:
    Code:
            for(Map<?, ?> entry : this.config.getMapList("messages"))
            {
                for(Map.Entry<?, ?> map : entry.entrySet())
                {
                   exampleHashMap.put((Integer) map.getKey(), (String) map.getValue());
                }
            }
    I am getting an NPE with the first line. Any ideas/help?
     
  2. this.config is null ?

    Still, I'm unsure if getMapList() would return what you need... sounds more like you need getConfigurationSection() and getKeys()... I dunno :-?
    Did you save it as a MapList or did you use config.set("messages.75", "This is a tset message.") (raw example) ?
     
  3. Offline

    dark navi

    Here is how I am saving the HashMap<Integer, String> object:
    Code:
    this.config.set("messages", exampleHashMap);
    If I so config.getConfigurationSection("messages").getKeys(), would it return all of the objects under messages, such as "messages.75" and "messages.100"?
     
  4. No, it will return all keys, "75", "100", etc, and you can use config.getString("messages." + key) to get the value.
    But if the getMapList() works you should use that.
     
  5. Offline

    dark navi

    It isn't working for me, I get an NPE. So I guess I'll try the roundabout way.
     
  6. I don't think you fully understand what a NPE is...
    It's simple actually, all variables are pointers to objects, if that variable is not pointing to any object it's null... therefore you can't call any methods on nothing.

    Example:
    null pointer exception code example (open)
    Code:
    List<String> test; // not asigned is the same as asigning it to null
    
    list.add("whatever"); // NPE here


    So, you either try and find that null pointer OR post the code (more code, where all variables are created and asigned) and I'll help :)
     
  7. Offline

    r0306

  8. Offline

    Sagacious_Zed Bukkit Docs

    You should be using getConfigurationSection("messages") then you can turn that into a hashmap or use it as is.
     
Thread Status:
Not open for further replies.

Share This Page