How to check if a config path doesn't exist?

Discussion in 'Spigot Plugin Development' started by rightfootmessi, Jun 4, 2017.

  1. Title says it all. Everything thus far that I've tried has shown no success.

    For specifics:
    Player data is going into the config file (ignore my methods, focus on helping me please). I have commands that will retrieve and/or alter the player data, but obviously if the player has never used the plugin before he will have no player data to retrieve and/or alter. I need to know how to check if the path doesn't exist, because I have the path set up as follows:
    Code (YAML):
    <player UUID>:
       - x
       - y
    : <ANY_MINECRAFT_SOUND as a string>
    : <between 0.5 and 2, but as string>
    To be EXTRA particular, I'm having problems detecting the alertwords field.

    I have variables that are set up as follows:
    pwords = puuid + ".alertwords";
    psound = puuid + ".sound";
    ppitch = puuid + ".pitch";

    The variable puuid is the player's UUID, which is set when they run the command so that I can access their data.

    Doing config.getStringList(pwords) anywhere will return null, leading to a NullPointerExceptions when I try to use it. Even if I try checking if config.getStringList(pwords) == null it still doesn't work, nor if I do config.getStringList(pwords).isEmpty(). However, using psound or ppitch DOES work.

    Help please? This is the final step in getting my plugin to work.
  2. You can do something like this:
    Code (Text):
    try {
    if (config.getStringList(pwords)) {
        //Do stuff here if it exists.
    } catch(NullPointerException e) {
        //Do stuff here if it doesn't exist.
    • Agree Agree x 1
  3. But you can't see if the NPE comes from your non-existing config value or something else inside your try block.

    Bukkit provides #isSet and all those #is<...type...> methods to check exactly this by a safe way... so just use them.
    • Agree Agree x 2
  4. if (config.isConfigurationSection(pwords)) {

    still causes the Exception.
  5. This may be a (dirty) solution:
    Code (Text):
    int bleh = config.getInt("bleh", 10298310);
    if (bleh == 10298310){
  6. I don't understand how that will help me.
  7. I feel stupid now.. You're talking about config paths without a value
    • Funny Funny x 2
  8. I was thinking of this once and I thought of this:

    Code (Text):
    public String getStringConfig(String configString) { /*you can change string to different values in a config*/
        return getConfig().getString(configString);

    My reasoning is that if it doesn't exists it will return null so you can basically call it then see if it's null but I haven't got around to trying it.
  9. Then "config" is null.
  10. But it's not.

    config is a static variable in that class file and is NOT null in every single other method. There is absolutely no reason that I know of that it would be null in this method.
    • Agree Agree x 1
  11. We could help you much better, if you would post the class and the stacktrace of the exception.
  12. You could always try my code.

    EDIT: or try .isSet instead of .isConfigurationSection or try both.
  13. Class:
    Code (Java):

    public class FileManager {
        public FileConfiguration config;
        public void activate() {
            config = Main.plugin.getConfig();
    // A lot of other methods in between, but those are irrelevant
        public void msgHasAlertWord(Player player, String msg) {
            UUID puuid = player.getUniqueId();
            String psound = puuid + ".sound";
            String ppitch = puuid + ".pitch";
            String pwords = puuid + ".alertwords";
            if (config.isConfigurationSection(pwords)) { //Line 158
                if (!config.getStringList(pwords).isEmpty()) {
                    boolean alerted = false;
                    if (!alerted) {
                        for (String alertword : config.getStringList(pwords)) {
                            if (msg.contains(alertword)) {
                                player.playSound(player.getLocation(), Sound.valueOf(config.getString(psound)), 1.0F, Float.valueOf(config.getString(ppitch)));
                                alerted = true;
    Code (Text):
    04.06 14:14:34 [Server] ERROR Could not pass event AsyncPlayerChatEvent to ZigAlerts v1.3.0
    04.06 14:14:34 [Server] INFO org.bukkit.event.EventException
    04.06 14:14:34 [Server] INFO at$1.execute( ~[custom.jar:git-Spigot-9797151-301db84]
    04.06 14:14:34 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent( ~[custom.jar:git-Spigot-9797151-301db84]
    04.06 14:14:34 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent( [custom.jar:git-Spigot-9797151-301db84]
    04.06 14:14:34 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent( [custom.jar:git-Spigot-9797151-301db84]
    04.06 14:14:34 [Server] INFO at [custom.jar:git-Spigot-9797151-301db84]
    04.06 14:14:34 [Server] INFO at net.minecraft.server.v1_10_R1.PlayerConnection.a( [custom.jar:git-Spigot-9797151-301db84]
    04.06 14:14:34 [Server] INFO at net.minecraft.server.v1_10_R1.PacketPlayInChat$ [custom.jar:git-Spigot-9797151-301db84]
    04.06 14:14:34 [Server] INFO at java.util.concurrent.Executors$ [?:1.8.0_111]
    04.06 14:14:34 [Server] INFO at [?:1.8.0_111]
    04.06 14:14:34 [Server] INFO at java.util.concurrent.ThreadPoolExecutor.runWorker( [?:1.8.0_111]
    04.06 14:14:34 [Server] INFO at java.util.concurrent.ThreadPoolExecutor$ [?:1.8.0_111]
    04.06 14:14:34 [Server] INFO at [?:1.8.0_111]
    04.06 14:14:34 [Server] INFO Caused by: java.lang.NullPointerException
    04.06 14:14:34 [Server] INFO at rfmessi.zigalerts.FileManager.msgHasAlertWord( ~[?:?]
    04.06 14:14:34 [Server] INFO at rfmessi.zigalerts.Alerter.AsyncPlayerChatEvent( ~[?:?]
    04.06 14:14:34 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
    04.06 14:14:34 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke( ~[?:1.8.0_111]
    04.06 14:14:34 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:1.8.0_111]
    04.06 14:14:34 [Server] INFO at java.lang.reflect.Method.invoke( ~[?:1.8.0_111]
    04.06 14:14:34 [Server] INFO at$1.execute( ~[custom.jar:git-Spigot-9797151-301db84]
    04.06 14:14:34 [Server] INFO ... 11 more
  14. config.isSet(puuid) to verify that they actually have an entry before you start trying to get subsets or values.
  15. Actually this is correct.
  16. Where's line 158?
    When and where gets #activate called? If #msgHasAlertWord gets called before #activate, this exception happens.
  17. Look at the code, I already labeled it.

    activate() is called when the plugin starts up, HOWEVER the event listener that calls msgHasAlertWord() is registered before activate(). Might that be the problem?
  18. Registring an event doesn't fire it. So it shouldn't be a problem as long as the event doesn't get fired by the server after you registered it and before you called #activate. But obviously this happens.
    So just make sure to call #activate before registering the listener.