Having a problem grabbing Values from Config.

Discussion in 'Spigot Plugin Development' started by Kozs, Dec 26, 2016.

  1. event.getPlayer().sendMessage(config.getString("message")); is what I have attempted to do.. It keeps throwing NPE errors like it can't read the file for some reason. Have made a lot of configs to know something is just odd here. Unless I am blatantly missing an obvious answer.

    Actual Code
    Code (Text):

        private FileConfiguration config = this.getConfig();
    @Override
           public void onEnable() {
               // Save a copy of the default config.yml if one is not there
               this.saveDefaultConfig();
       
               // Register a new listener
               getServer().getPluginManager().registerEvents(new Listener() {
           
                   @EventHandler
                   public void playerJoin(PlayerJoinEvent event) {
                       // On player join send them the message from config.yml
                       event.getPlayer().sendMessage(config.getString("message"));
                   }
               }, this);

               // Set the command executor for the rules command
               this.getCommand("rules").setExecutor(new CommandExecutor() {
           
                   public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                       // On command send the rules from config.yml to the sender of the command
                       List<String> rules = config.getStringList("rules");
                       for (String s : rules){
                           sender.sendMessage(s);
                       }
                       return true;
                   }
               });
           }
     

    Error
    First Error - JoinEvent
    Second Error - me typing /rules in chat
    Code (Text):

    [03:14:48 ERROR]: Could not pass event PlayerJoinEvent to VF v1.2.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[Server.jar:git-Spigot-7d78b81-50acb44]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Server.jar:git-Spigot-7d78b81-50acb44]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [Server.jar:git-Spigot-7d78b81-50acb44]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.PlayerList.onPlayerJoin(PlayerList.java:327) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.PlayerList.a(PlayerList.java:160) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.LoginListener.b(LoginListener.java:147) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.LoginListener.F_(LoginListener.java:57) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.NetworkManager.a(NetworkManager.java:233) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.ServerConnection.c(ServerConnection.java:140) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:834) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [Server.jar:git-Spigot-7d78b81-50acb44]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
    Caused by: java.lang.NullPointerException
            at org.kozs.vf.main.MainFly$1.playerJoin(MainFly.java:43) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_65]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_65]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_65]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_65]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[Server.jar:git-Spigot-7d78b81-50acb44]
            ... 14 more

    [03:16:14 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'rules' in plugin VF v1.2.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Server.jar:git-Spigot-7d78b81-50acb44]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[Server.jar:git-Spigot-7d78b81-50acb44]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:649) ~[Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1344) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1179) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [Server.jar:git-Spigot-7d78b81-50acb44]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_65]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_65]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [Server.jar:git-Spigot-7d78b81-50acb44]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [Server.jar:git-Spigot-7d78b81-50acb44]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
    Caused by: java.lang.NullPointerException
            at org.kozs.vf.main.MainFly$2.onCommand(MainFly.java:52) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Server.jar:git-Spigot-7d78b81-50acb44]
            ... 15 more
     
    MainFly.java:43 - event.getPlayer().sendMessage(config.getString("message"));
    MainFly:52 - List<String> rules = config.getStringList("rules");
    Config.yml
    Code (Text):

    # default config.yml
    message: Hello World and Welcome! :)
    rules:
      - Play Nice
      - Respect others
      - Have Fun
      - Dont swear
     
    Just made a topic and had a similiar issue but I had that issue because I was trying to fix this issue. Now I just still have this issue :p
     
    #1 Kozs, Dec 26, 2016
    Last edited: Dec 26, 2016
  2. That doesn't look like the whole error
     
  3. I posted whole error, and the error line numbers of which the main issue is coming from, both pointing at the config var methods.
     
    #3 Kozs, Dec 26, 2016
    Last edited: Dec 26, 2016
  4. It seem like you haven't initialized your config variable. Is that possible?o_O
     
  5. No I did with the method

    private FileConfiguration config = getConfig(); //OR whatever the proper names are.


    Still looking for incite, thanks y'all
     
  6. For strings in config normally you need to put then into quotes 'string here'
     
  7. Code (Text):

    # default config.yml
    message: 'Hello World and Welcome! :)'
    rules:
      - 'Play Nice'
      - 'Respect others'
      - 'Have Fun'
      - 'Dont swear'
     
    Used this new Config, didn't work. Neither one of them. TBH I never used quotes for my other projects Config DOES work.
     
  8. Try this debugging code:
    Code (Java):
           if (config != null) {
                if (config.getString("message") != null) {
                    event.getPlayer().sendMessage(config.getString("message"));
                } else {
                    getLogger().info("config#getString(\"message\") = null");
                }
            } else {
                getLogger().info("config = null");
            }
     
  9. The output was getLogger().info("config = null");
     
  10. Code (Text):
    this.getConfig();
    this.saveDefaultConfig();
    this.getCommand("rules").setExecutor(new CommandExecutor() {
    Since your class extends JavaPlugin, 'this.' is unnecessary.
    Try removing those and see if it works.
     
  11. Nah that isn't the problem, thanks though.
     
  12. Ah I looked again, it might be caused because you define
    Code (Text):
    private FileConfiguration config = this.getConfig();
    outside of any methods. Try this:
    Code (Text):
    private FileConfiguration config;
    @Override
           public void onEnable() {
              config = this.getConfig();
    ...
     
  13. Already tried that too.. I even compared the debug with

    Code (Text):

                       if (getConfig() != null) {
                           if (config.getString("message") != null) {
                               event.getPlayer().sendMessage(config.getString("message"));
                           } else {
                               getLogger().info("config#getString(\"message\") = null");
                           }
                       } else {
                           getLogger().info("config = null");
                       }
    getConfig returns null even.
     
  14. You could try saving the config in another fashion:
    Code (Text):
    getDataFolder().mkdirs();
    getConfig().options().copyDefaults(true);
    saveConfig();
     
  15. ...and by the way try not to write like this, it's really confusing :(
    Code should look organised and under control.

    It's your decision

    But to the problem:

    Code (Text):
    Caused by: java.lang.NullPointerException
            at org.kozs.vf.main.MainFly$1.playerJoin(MainFly.java:43) ~[?:?]
    rather the config has for "message" null or your Configuration Object is null :D

    Try what @Rsl1122 said.
     
    • Agree Agree x 1