Solved Get String with brackets from config

Discussion in 'Spigot Plugin Development' started by Minimigamer, Apr 2, 2020.

  1. Hey im trying to save a String in the config:


    it saves the String in the config as: '[SomeString]: '

    but when i try to get the String it throws a NullPointerException.
    Does anyone know a soulution ?

    Code (Text):

    messagesCFG.set("prefix", "[SomeString]: ");

    prefix = messagesCFG.getString("prefix") + ChatColor.RED;
     
  2. You have to save the config after making changes to it if I’m not wrong.
     
  3. You have to save your config file after you make all the changes you want to make.
    After all changes to the file, remember to write messageCFD.save();
     
  4. yea i saved it and it saved successfully like i said: it saves the String in the config as: '[SomeString]: '
    when you open the config file you see exactly: prefix: '[SomeString]: '


    sorry if that wasn't clear
     
    #4 Minimigamer, Apr 2, 2020
    Last edited: Apr 2, 2020
  5. You should post your full code for better understanding.

    Also maybe try out
    Code (Text):
    messagesCFG.getString("prefix", “debug default value”);
    for debugging. Like this you can get the config value and if that doesn’t exist you will get a default value instead, in this case the default would be “debug default value”.

    This could be useful to find out if the NullPointer is really caused by the missing value in the config or by something else.
     
  6. drives_a_ford

    Moderator

    Showing your actual error message would be helpful.

    And showing your code would be helpful as well.

    If the line you did include is where the NullPointerException is thrown, then this would imply that messagesCFG is null.
     
    • Agree Agree x 1
  7. I tried if(messageCFG == null) { Bukkit.getServer().broadcastMessage("null");
    but here is the complete code:
    Code (Text):


    public class FileManager {
        private static File dir = new File("plugins/UltimateTroll");
        private static File settings = new File(dir.getPath() + "/Settings.txt");
        private static File messages = new File(dir.getPath() + "/Messages.txt");
        public static FileConfiguration settingsCFG;
        public static FileConfiguration messagesCFG;
       
        public static List<UUID> trolling;
        public static Player target = null;
        public static HashMap<UUID, ItemStack[]> inventory;
        public static String prefix = "";
        public static String notEnabled = "";
        public static String notaPlayer = "";
        public static String noperm = "";
        public static String pNotFound = "";
       
       
        public static void setup() {
           
            if(!dir.exists()) {
                dir.mkdir();
                try {
                    settings.createNewFile();
                    settingsCFG = YamlConfiguration.loadConfiguration(settings);
                    setSettings();
                   
                   
                    messages.createNewFile();
                    messagesCFG = YamlConfiguration.loadConfiguration(messages);
                    setMessages();
                   
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        private static void setSettings() {
            if(settingsCFG == null) {
                return;
            }
            boolean enabled = true;
            settingsCFG.set("TrollSystem", enabled);
            settingsCFG.set("crash -- ", enabled);
            try {
                settingsCFG.save(settings);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       
        private static void setMessages() {
            messagesCFG.set("prefix", "#[UltimateTroll]: #");
            messagesCFG.set("notEnabled", "This Feature is not enabled!");
            messagesCFG.set("start", "Ultimate Troll is now active!");
            messagesCFG.set("Not a Player", "You have to be a Player to perform this command!");
            messagesCFG.set("No permission", "You are not permitted to perform this command!");
            messagesCFG.set("Player not found", "Player not found!");
            try {
                messagesCFG.save(messages);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static void loadVariables() {
           
            notEnabled = messagesCFG.getString("notEnabled");
            prefix = messagesCFG.getString("prefix") + ChatColor.RED;
            notaPlayer = prefix + messagesCFG.getString("Not a Player");
            noperm = prefix + messagesCFG.getString("no permission");
            pNotFound = prefix + messagesCFG.getString("Player not found");
        }
    }
     
  8. drives_a_ford

    Moderator

    The code is a nest of static abuse. Please take a look at Beginner Programming Mistakes and Why You're Making Them.


    But your issue is the fact that the #setup method only initializes the variables if the plugin's data folder does not exist. Otherwise all the static fields remain null. And that's most likely why the field is in fact null like I said it would be.