Solved NPE from getConfigurationSection()

Discussion in 'Plugin Development' started by Etsijä, May 15, 2014.

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

    Etsijä

    I have a datafile for my plugin, and the structure is
    Code:
    '1':
      world: Pelitcraft
      x: 1540.0
      y: 69.0
      z: 3163.0
      direction: -45
      angle: 33
      power: 1.5
    '2':
      world: Pelitcraft
      x: 1542.0
      y: 69.0
      z: 3162.0
      direction: -9
      angle: 54
      power: 1.5
    ...
    I'm trying to read this in with
    Code:java
    1. public void loadCannonsYML() {
    2. File saveFile = new File(this.getDataFolder(), "cannons.yml");
    3. if (saveFile.exists()) {
    4. try {
    5. cannonsYML.load(saveFile);
    6. } catch (Exception e) {
    7. e.printStackTrace();
    8. }
    9. } else {
    10. return;
    11. }
    12. int i = 1;
    13. while (true) {
    14. ConfigurationSection cs = cannonsYML.getConfigurationSection(Integer.toString(i));
    15. if (cs == null) {
    16. return;
    17. }
    18. World w = Bukkit.getServer().getWorld(cs.getString("world"));
    19. Double x = cs.getDouble("x");
    20. Double y = cs.getDouble("y");
    21. Double z = cs.getDouble("z");
    22. int direction = cs.getInt("direction");
    23. int angle = cs.getInt("angle");
    24. float power = (float) cs.getDouble("power");
    25. Location loc = new Location(w, x, y, z);
    26. CannonDispenser cannon = new CannonDispenser(direction, angle, power);
    27. cannons.put(loc, cannon);
    28. _log.info("cannon: " + cannons.get(loc));
    29. i++;
    30. }
    31. }


    but I'm getting an NPE on line 14 the first time the loop is entered, so nothing gets read in. Obviously something wrong with the invoke of getConfigurationSection() but I cannot figure out what it is, since the opposite - saving an YML file with a similar method - works.

    EDIT: I've made sure that IF cannons.yml exists at all, it will ALWAYS include at least the configuration section '1'.
     
  2. Etsijä cannonsYML is null. Also, two possibilities: Your loop does nothing or your server does nothing (infinite loop)
     
    Etsijä likes this.
  3. Offline

    Etsijä

    Of course! Forgot to add "cannonsYML = new YamlConfiguration();" Thanks for a quick (and correct) answer, Adam! I'll go bang my head on the wall now.
     
    AdamQpzm likes this.
  4. Etsijä No problem, and don't forget about that useless-or-infinite loop ;)
     
  5. Offline

    Etsijä

    Well that works, since the loop is exited when the next configuration section doesn't exist (ie. we just read in the last one in the file).
     
  6. Etsijä Ah yes, that's true, sorry about that. I think I read cs as something else in your while loop :p
     
Thread Status:
Not open for further replies.

Share This Page