[Solved] Reading from config.yml returns null.

Discussion in 'Spigot Plugin Development' started by MTM123, Mar 29, 2015.

  1. Maybe the problem is "LPSPlayerHider"?
  2. Indeed it is the problem because:
    Code (Text):
    String name = plugin.getConfig().getString("Player_Hider.on.name").replace('&', ChatColor.COLOR_CHAR);
    returns null.
    • Like Like x 1
    • Agree Agree x 1
  3. Chances are that your key is wrong. Delete the saved config and run your plugin again to generate a fresh one.
  4. try maybe to debug the playerjoinevent and check which lines its errors you?
  5. If plugin.getConfig().getString("Player_Hider.on.name") returns null, String.replace() won't work, because it works only on a string, not on null. The replace() will probably throw a NullPointerException if the string with which it should work, is null.

    Check what the string is and if it exists before performing operations on it like String.replace(). Also, as @iXColtXi said, ChatColor.translateAlternateColorCodes() is pretty useful.
    Code (Text):

    String configString = plugin.getConfig().getString("Player_Hider.on.name");
    if(configString != null) {
        String name = ChatColor.translateAlternateColorCodes('&', configString);
        System.out.println("Name is: " + name);
    else {
        System.out.println("Error: name is null, can't find it in the config!");
  6. Actually, that piece of code wont do any good.

    See, you are defining a variable as the (possible) null string before checking if it is null or not.

    You could do something like:
    if(getConfig().getString("boop") != null) {
      // yay
      String name = getConfig().getString("boop");
    } else {
      // boo
    Code (Text):
    try {
      String name = getConfig().getString("boop");
    } catch (NullPointerException e1) {
      // boo
  7. I have tried that before still returned null.

    I always do that on test server when I update config or code.

    line 44 on LSPlayerHider. getting string returns null.

    Yes I get that i have tried to get string without replace still throws null.
  8. Error handling is fine but still I don't see reason why it doesn't return sring.
  9. That's actually the same. If config.getString("blah") is indeed null, then configString is assigned a null value. Then, you check if configString is not null. If so, you can use that string. If it's null, you can't. That way, you won't have to plugin.getConfig().getString() twice. The results would indeed be the same.

    Apparently plugin.getConfig().getString("Player_Hider.on.name") is null. The required config.yml would be something like:
    Code (Text):

        name: Some string here
    What does your config.yml look like?
  10. It's at first post but ok:
    config - http://pastebin.com/z8Vgk8Pe
    • Friendly Friendly x 1
  11. Ah, I didn't notice o_O The config is obviously fine because Player_Hider.on.item_id didn't throw an error.
    Hmm, strange! Maybe ChatColor.COLOR_CHAR is null?
  12. @MTM123
    Try to move
    Code (Java):
    Code (Java):
    phider = new LSPlayerHider(plugin);
  13. Nope. Still returns null.
  14. Have used it before in same plugin actually it works fine. I have tried even without replacing '&' or translatingAlternateColorCodes() still throwed null.
  15. Hello, Just a *note*, Your Error pastebin has been deleted. Can you repost it?
    Also, Does it return NULL with other Strings / Booleans ?
    • Like Like x 1
  16. So i fixed the problem.

    The main problem was:
    YAML Converts "yes , no , on , and off" to "true, false"
    How was this a problem in your case?

    Well if you put your config file inside of a YAML parser you can see what it actually does:

    http://yaml-online-parser.appspot.com/?yaml=Player_Hider: enabled: true slot: 3 on: item_id: 381 name: '&6Player_Hider' lore: - '&2Click to show/hide players!' chatmsg: '&2Showing players' off: item_id: 368 name: '&6Player_Hider' lore: - '&2Click to show/hide players!' chatmsg: '&5Hiding players' cooldown: 5&type=json

    So i renamed those to ItemOn and ItemOff. And see what it outputs now:

    Also your onInteract function was not working. So i fixed that one too. :)

    I didn't touch this one ;)


    • Winner Winner x 1
    • Informative Informative x 1
  17. Actually I noticed changes in my config one time. "on" was true and "off" was false. I didn't know why. Thanks for your effort and time helping to solve this issue. :)
    • Like Like x 1
    • Winner Winner x 1
  18. No problem! Enjoy programming ! :)
    • Like Like x 1
    • Agree Agree x 1