Solved Count config list

Discussion in 'Plugin Development' started by DS1995, Sep 23, 2013.

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

    DS1995

    Hello everybody,
    I am making a plugin, but the connection between the plugin and the .yml file doesn't work correct. I want to count the number of 'treasures' that are in the .yml.
    This is a part of the class where it must count:
    Code:java
    1. List<?> list = Treasure.yaml.getList("Treasures");
    2. int count = list.size();

    The yaml is connect to the main class where it is connected to the file
    Code:java
    1. YamlConfiguration yaml;
    2. File config = new File("plugins/Treasure/Config.yml");
    3. yaml = YamlConfiguration.loadConfiguration(config);

    The .yml file is like this:
    Treasures:
    '1':
    items:
    '22': 1

    But when I want it to count (answer is 1), it says a nullpointer. How to fix it?
     
  2. Offline

    Mitsugaru

    Well... this is assuming the actual format is as follows:
    Code:
    Treasures:
        '1':
    items:
        '22': 1
    Please correct me if I'm wrong on this assumption.

    If the above is true, then the reason you're getting a null pointer is because Treasures is not a List of items. Rather, Treasures is a ConfigurationSection.

    Having no knowledge of the original intent, I can only give the following advice, either:

    A) Make Treasures a normal key with a value and just get the integer that you wanted.
    Code:
    Treasures: 1

    B) Make Treasures a list.
    Code:
    Treasures:
      - 1
      - 3
      - 8
    C) Keep it as a ConfigurationSection and just get the size of keys.
    Code:
    int treasures = Treasure.yaml.getConfigurationSection("Treasures").getKeys(false).size();
    No idea what you're planning to do with the Treasures section... so, just pick what works for your format.
     
  3. Offline

    DS1995

    Thank you! It worked!
     
Thread Status:
Not open for further replies.

Share This Page