Solved check if config contains playername

Discussion in 'Plugin Development' started by baenii, Nov 2, 2019.

Thread Status:
Not open for further replies.
  1. Hej everyone.
    Today I have a problem checkinf if the config contains a playername.

    My current code:
    Code:
    if(!(Main.getInstance().getConfig().getConfigurationSection("Chatban").getKeys(false).contains(targetName))) {
                          
         Main.getInstance().getConfig().set("Chatban." + targetName, time);
         Main.getInstance().saveConfig();
                          
    }
    With this I'm getting an internal error.
     
  2. Offline

    robertlit

    Post the stacktrace and mention what line is the error in.
     
  3. @robertlit of course, sorry.. The error occurs in line 86 which is the following line:
    Code:
    if(!(Main.getInstance().getConfig().getConfigurationSection("Chatban").getKeys(false).contains(targetName))) {
    
     
  4. Offline

    robertlit

    I am guessing it is a NPE, so either Main.getInstance() is null, or the configuration section is null or targetName is null
     
    baenii likes this.
  5. targetName cannot be null as its just args[0] of the command so that cannot be the issue.
    the configuration section can of course be null but this is what I am checking there, isn't it?
    And the Main Instance looks like that:
    Code:
    private static Main instance;
    public static Main getInstance() { return instance; }
    
    //EDIT
    I actually do not check if the configuration section is null or not but if it contains an object. So the configuration section is of course null cuz I never added it. How can I add it?
     
  6. Offline

    robertlit

    No, you're checking if the section contains a value, not if ot exists.
    The Main instance is null in your case since you don't assign a value to it. You should do private Main instance = this;
    Also I would recommend using dependency injection instead of singleton.
    Edit: noticed your edit.
    You can't just add a configuration section, you will have to set a value inside of it.
    What you should do instead of checking if the configuration section contains the name is check if Main.getInstance().getConfig().get("sectionname."+targetName)!=null
     
    Last edited: Nov 2, 2019
    baenii likes this.
  7. Omg I forgot to add instance = this into onEnable()... My fault. But anyways, this is not solving my problem yet.

    //EDIT:
    I now changed my code to check if the configuration section is null and it fixed it now. So these two things were the issues. Thanks!
     
  8. Offline

    robertlit

    Glad I could help. Mark youe thread solved :)
     
    baenii likes this.
Thread Status:
Not open for further replies.

Share This Page