[SOLVED] Config.getString() sometimes returns null when the path *is* there

Discussion in 'Plugin Development' started by caldabeast, Jun 5, 2012.

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

    caldabeast

    I have to parts of code. The only difference between them is variable names and where in the config they direct to:

    Code:
    public static String getName(String name){
            String nameColor = "";
            String preset = "";
            nameColor = config.getString("olyChat.Users." + name + ".NameColor");
            if(nameColor == null){
                preset = config.getString("olyChat.Users." + name + ".Preset");
                if(preset == null){
                    nameColor = config.getString("olyChat.Defaults.NameColor");
                }else{
                    nameColor = config.getString("olyChat.Presets." + preset + ".NameColor");
                    if(nameColor == null){
                        nameColor = config.getString("olyChat.Defaults.NameColor");
                    }
                }
            }return formatColors(nameColor).replace("%name", name);
        }
    Code:
    public static String getChatColor(String name){
            String chatColor = "";
            String preset = "";
            chatColor = config.getString("olyChat.Users." + name + ".ChatColor");
            if(chatColor == null){
                preset = config.getString("olyChat.Users." + name + ".Preset");
                if(preset == null){
                    chatColor = config.getString("olyChat.Defaults.ChatColor");
                }else{
                    chatColor = config.getString("olyChat.Presets." + preset + ".ChatColor");
                    if(chatColor == null){
                        chatColor = config.getString("olyChat.Defaults.ChatColor");
                    }
                }
            }return formatColors(chatColor);
        }
    The first one *always* works, but in the second one, the first check returns null, and goes onto the next checks, even though the variable *is* there.

    My config.yml:
    Code:
    olyChat:
        Defaults:
            NameColor: '&7%name'
            ChatColor: '&f'
            SeparatorColor: '&8'
        Users:
            -snip-
            CalDaBeast:
                NameColor: '&cCal&dDa&bBeast'
                ChatColor: '&3'
                SeparatorColor: '&a'
        - snip-
        #Presets would go here, but they work properly, so I took them out of the post
    
    Any help would be greatly appreciated, because i've been sitting here for nearly three hours trying to get this to work....
     
  2. Offline

    CorrieKay

    you -snip-ed the Users configuration section. youre using getString() on it, however, im fairly sure theres supposed to be multiple entries there. Is it a string list? if so, this would be the cause of getString returning null, because its not a string, but a string list.

    edit: my mistake, its not a key/value, but a configsection.

    Try this debug:

    System.out.print("olyChat.Users." + name + ".ChatColor");

    make sure it matches up with what you think it should be. Ive made this mistake tons of times, and it typically ends up being a wrong path string :p
     
    caldabeast likes this.
  3. Offline

    caldabeast

    CorrieKay
    OH MY GOSH! THANK YOU!
    You solved my problem!
    What was happening was that I was setting the name variable to the person's name color and THEN checking other variables off of that, so it would check for "olyChat.Users.&cCal&dDa&bBeast.ChatColor". Made it so it checks for name last and now it should work! Thank you so much!
     
    CorrieKay likes this.
  4. Offline

    CorrieKay

    glad to help, and like i said, in the future, always check your config string paths. Theyre a bugger...

    :D
     
Thread Status:
Not open for further replies.

Share This Page