Solved Custom Config getDouble() errors

Discussion in 'Spigot Plugin Development' started by S1ant, Mar 28, 2020.

  1. Hello, so I've just got something to work with a normal config, and then I switch over to a custom config, and the same exact thing doesn't work. Here are snippets of code, I have a feeling it may be an error with saving, if so please let me know, thanks!

    main class:
    Code (Java):
    private File customConfig;
    private FileConfiguration conf;

    public void onEnable(){
    createCustomConfig();
            getCustomConfig().options().copyDefaults(true);
    }


    private void createCustomConfig(){
            customConfig = new File(getDataFolder(), "parkour.yml");
            if(!customConfig.exists()){
                customConfig.getParentFile().mkdirs();
                saveResource("parkour.yml", false);
            }

            conf = new YamlConfiguration();
            try {
                conf.load(customConfig);
            } catch(IOException | InvalidConfigurationException e){
                e.printStackTrace();
            }
        }

        public FileConfiguration getCustomConfig(){
            return this.conf;
        }

     
    The class where I'm attempting to get the Double from the config:

    Code (Java):
    private FileConfiguration config = main.getInstance().getCustomConfig();

    //This the line 23 that is receiving the error
    p.teleport(new Location(p.getWorld(), config.getDouble(i+".x"), config.getDouble(i+".y"), config.getDouble(i+".z")));

     
    The parkour.yml configuration file:

    Code (YAML):
    # Each number is an entity.
    1
    :
      x
    : 0.0
      y
    : 100.0
      z
    : 0.0
      yaw
    : 90.0
      pitch
    : 0.0
      name
    : "&c&lStart Parkour"
    2
    :
      x
    : 0.0
      y
    : 100.0
      z
    : 2.0
      yaw
    : 90.0
      pitch
    : 0.0
      name
    : "&1&lCheckpoint 1"
    3
    :
      x
    : 0.0
      y
    : 100.0
      z
    : 4.0
      yaw
    : 90.0
      pitch
    : 0.0
      name
    : "&1&lCheckpoint 2"
    4
    :
      x
    : 0.0
      y
    : 100.0
      z
    : 6.0
      yaw
    : 90.0
      pitch
    : 0.0
      name
    : "&1&lCheckpoint 3"
    5
    :
      x
    : 0.0
      y
    : 100.0
      z
    : 8.0
      yaw
    : 90.0
      pitch
    : 0.0
      name
    : "&b&lEnd"
    spawn
    :
      x
    : 1.5
      y
    : 100.0
      z
    : -0.5
    The line I outlined above is receiving a NullPointerException, even though it is valid.
    I have also checked the int, i, to make sure it was 1-5 and it is every time. So I really have no idea what's going on here I've seen multiple threads that talk about not being able to use getDouble() with a custom config, but no answers out there.
     
  2. drives_a_ford

    Moderator

    Either p is null or config is null. Print them both out before the offending line and you'll know which one it is.
     
    • Like Like x 1
  3. Thanks, the config is null. But do you have any idea why? I accessed it at the top of the code as you can see, and the method worked perfectly from inside of the main class, but returns null from another class?
     
  4. drives_a_ford

    Moderator

    This is likely because you're creating an instance of the class which uses this before createCustomConfig is ever called.

    When are you creating this instance? Since you're calling the createCustomConfig method from onEnable, (which initializes the config field in the JavaPlugin extension), then you're likely creating the instance before onEnable gets called (or before the createCustomConfig method gets called within onEnable if you removed some of the code from there).
     
    • Useful Useful x 1
  5. Thank you so much! All I had to do was move createCustomConfig() to the top of my onEnable method. Thanks!