Getting config list returns [null]

Discussion in 'Plugin Development' started by kps1796, Aug 5, 2014.

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

    kps1796

    I've made a SerializableLocation class:
    Code:java
    1. package io.github.tubakyle.clashjoin;
    2. import org.bukkit.Bukkit;
    3. import org.bukkit.Location;
    4. import org.bukkit.configuration.serialization.ConfigurationSerializable;
    5.  
    6. import java.util.HashMap;
    7. import java.util.Map;
    8.  
    9. public class SerializableLocation implements ConfigurationSerializable {
    10. private String worldName;
    11. private double x, y, z;
    12.  
    13. public SerializableLocation(Location location) {
    14. this(location.getWorld().getName(), location.getX(), location.getY(), location.getZ());
    15. }
    16.  
    17. public SerializableLocation(String worldName, double x, double y, double z) {
    18. this.worldName = worldName;
    19. this.x = x;
    20. this.y = y;
    21. this.z = z;
    22. }
    23.  
    24. public Location getLocation() {
    25. return new Location(Bukkit.getWorld(worldName), x, y, z);
    26. }
    27.  
    28. public void setWorldName(String worldName) {
    29. this.worldName = worldName;
    30. }
    31.  
    32. public void setX(double x) {
    33. this.x = x;
    34. }
    35.  
    36. public void setY(double y) {
    37. this.y = y;
    38. }
    39.  
    40. public void setZ(double z) {
    41. this.z = z;
    42. }
    43.  
    44. public double getX() {
    45. return x;
    46. }
    47.  
    48. public double getY() {
    49. return y;
    50. }
    51.  
    52. public double getZ() {
    53. return z;
    54. }
    55.  
    56. public String getWorldName() {
    57. return worldName;
    58. }
    59.  
    60. @Override
    61. public Map<String, Object> serialize() {
    62. Map<String, Object> map = new HashMap<String, Object>(4);
    63.  
    64. map.put("world", worldName);
    65. map.put("x", x);
    66. map.put("y", y);
    67. map.put("z", z);
    68.  
    69. return map;
    70. }
    71. }
    72.  

    When saving a List<SerializableLocation> to the config, everything works fine.
    Here's the config after testing a sign:
    Code:
    signs:
    - ==: io.github.tubakyle.clashjoin.SerializableLocation
      x: 10.0
      y: 125.0
      z: 2.0
      world: world
    
    Here's a part of onEnable:
    Code:java
    1. private List<SerializableLocation> signs;
    2. signs = new ArrayList<SerializableLocation>();
    3. if(getConfig().isList("signs")) {
    4. getLogger().info(getConfig().get("signs").toString());
    5. getLogger().info(((List<SerializableLocation>) getConfig().getList("signs")).toString());
    6. List<SerializableLocation> serializedLocations = (List<SerializableLocation>) getConfig().getList("signs");
    7. for (SerializableLocation serializedLocation : serializedLocations) {
    8. getLogger().info(signs.toString());
    9. signs.add(serializedLocation);
    10. getLogger().info(signs.toString());
    11. }
    12. }

    Console output with above config:
    Code:
    4:04:44 PM ClashJoin: [null]
    4:04:44 PM ClashJoin: [null]
     
    4:04:44 PM ClashJoin: []
    4:04:44 PM ClashJoin: [null]
    Why would the list be [null] when it contains items?

    xTigerRebornx You're good at this kind of stuff. Any clues as to what's wrong?

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

    xTigerRebornx

    kps1796 Post your full main class.
     
  3. Offline

    kps1796

    Last edited by a moderator: Jun 9, 2016
  4. Offline

    xTigerRebornx

    kps1796 Did you run the test when there was no config generated yet?
     
  5. Offline

    kps1796

    Well I just tried that and I get a NullPointerException.
    EDIT: It only needs to work when the config is generated

    xTigerRebornx
    Code:java
    1. getLogger().info(getConfig().toString())
    returns
    Code:
    YamlConfiguration[path='', root='YamlConfiguration']
    a blank config?
    it isn't blank.

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

    xTigerRebornx

    kps1796 You just called toString() on the config, get the keys and loop through those, printing each element
     
  7. Offline

    kps1796

    xTigerRebornx It doesn't show any keys after "signs"

    xTigerRebornx It all of a sudden started working! Thanks for the help.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page