Solved Config returning null

Discussion in 'Plugin Development' started by MrDaniel, Jan 10, 2020.

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

    MrDaniel

    My code
    code (open)

    Code:
    package ladeira.attributes;
    
    import java.util.HashSet;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    
    import ladeira.general.ServerHandler;
    
    public class AttributesHandler {
       
        private static HashSet<Permission> permissions = new HashSet<Permission>();
       
        public static void loadAttributes() {
            loadPermissions();
        }
       
        public static Permission findPerm(int id) {
            for (Permission i: permissions) {
                if (i.getID() == id) {
                    return i;
                }
            }
            return null;
        }
       
        private static void loadPermissions() {
            FileConfiguration config = ServerHandler.getAttrConfig();
           
            for (String key : config.getConfigurationSection("permissions").getKeys(false)) {
                ServerHandler.getPlugin().getLogger().severe(ServerHandler.getAttrConfig().getString(key + ".nameColor"));
                int id = config.getInt(key + ".id");
                ChatColor color = getChatColor(config.getString(key + ".nameColor"));
                ChatColor chatColor = getChatColor(config.getString(key + ".chatColor"));
               
                permissions.add(new Permission(id, key, "permissions." + key, color, chatColor));
            }
        }
       
        private static ChatColor getChatColor(String text) {
            ChatColor color = ChatColor.WHITE;
           
            switch (text) {
            case "aqua":
                color = ChatColor.AQUA;
                break;
            case "black":
                color = ChatColor.BLACK;
                break;
            case "blue":
                color = ChatColor.BLUE;
                break;
            case "dark_aqua":
                color = ChatColor.DARK_AQUA;
                break;
            case "dark_blue":
                color = ChatColor.DARK_BLUE;
                break;
            case "dark_gray":
                color = ChatColor.DARK_GRAY;
                break;
            case "dark_green":
                color = ChatColor.DARK_GREEN;
                break;
            case "dark_purple":
                color = ChatColor.DARK_PURPLE;
                break;
            case "dark_red":
                color = ChatColor.DARK_RED;
                break;
            case "gold":
                color = ChatColor.GOLD;
                break;
            case "gray":
                color = ChatColor.GRAY;
                break;
            case "green":
                color = ChatColor.GREEN;
                break;
            case "light_purple":
                color = ChatColor.LIGHT_PURPLE;
                break;
            case "red":
                color = ChatColor.RED;
                break;
            case "white":
                color = ChatColor.WHITE;
                break;
            case "yellow":
                color = ChatColor.YELLOW;
                break;
            }
           
            return color;
        }
    }


    Basically, the problem is that the error is on the line that says switch (text) { says that it is a null pointer exceptoin, and the line ChatColor color = getChatColor(config.getString(key + ".nameColor")); has a null value, but why I am confused is because int id = config.getInt(key + ".id"); works.

    pls help, i have been trying to fix this for 30min
     
  2. Offline

    KarimAKL

    @MrDaniel
    1. config.getString(String) is returning null, does the config contain the path?
    2. For your getChatColor(String) method, you can just do something like this:
    Code:Java
    1. private static ChatColor getChatColor(String text) {
    2. ChatColor color = null; // Or whatever you want it to return by default
    3. try {
    4. color = ChatColor.valueOf(text.toUpperCase());
    5. return color;
    6. }
    7. return color;
    8. }
     
  3. Offline

    MrDaniel

    @KarimAKL,

    1. I double checked, the config exists, and the path is correct
    2. OMG, yes, thank you, I knew there was a way!

    EDIT: Ok, I did some debugging and I found out that the int id was also null, but it didn't point out an error, the only problem I have rn is getting the config then. For now Imma do some more debugging and try to find the problem myself, imma edit this if I have an answer or if I find something more

    EDIT2: I found it out, FINNALY. The problem was that the id was at permissions.owner.id and I was looking at owner.id, that's why it was returning null
     
    Last edited: Jan 11, 2020
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page