Solved Problems with getting values of config

Discussion in 'Plugin Development' started by sebagius7110, Aug 11, 2015.

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

    sebagius7110

    Hello, today I was working on adding a config to customize messages for my plugin BetterVanish. But Whenever I try to get a value for example a String I get this error:

    Error (open)

    [17:34:14] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'v' in plugin BetterVanish v1.1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:640) ~[spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_80]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_80]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    at java.lang.Thread.run(Unknown Source) [?:1.7.0_80]
    Caused by: java.lang.NullPointerException
    at sebagius7110.BetterVanish.Vanish.toggleVanish(Vanish.java:26) ~[?:?]
    at sebagius7110.BetterVanish.Vanish.onCommand(Vanish.java:116) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.7.jar:git-Spigot-f94fe8f-d27e6d0]
    ... 15 more


    Code I'm using to getConfig String:

    Code:
    Main.main.getConfig().getString("messages.plugin.vanish.enabled");
    Main does extend Java Plugin and I have an instance of the class as follows:

    Code:
    public static Main main;
     
  2. Offline

    SuperSniper

    at sebagius7110.BetterVanish.Vanish.toggleVanish(Vanish.java:26) ~[?:?]
    at sebagius7110.BetterVanish.Vanish.onCommand(Vanish.java:116) ~[?:?]

    Show us line 26 & line 116 of your Vanish class.

    This doesn't seem like a problem with your config, its more towards a problem with your code overall.
     
  3. Offline

    sebagius7110

    String enabledMessage = Main.main.getConfig().getString("messages.plugin.vanish.enabled"); (26)

    toggleVanish(p); (116)

    Line 116 links to line 26. And yes it is config :p or maybe how I specified it.

    Also if I just use the code to get the config 'rawly' not creating a variable to store it, it will get an error there as well

    Bump!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  4. Offline

    sebagius7110

  5. Offline

    sebagius7110

    Here:

    Config (open)

    Code:
    messages:
      plugin:
        vanish:
          enabled: 'Vanish Enabled For: '
          disabled: 'Vanish Disabled For: '
      api:
        vanish:
          enabled: 'Vanish Enabled For: '
          disabled: 'Vanish Disabled For: '


    Need to go school, cya for now.
     
    Last edited: Aug 12, 2015
  6. @sebagius7110 Hi, you should try to replace ' with ".

    @sebagius7110 Can you show us how you defined p ? Showing your code will be easier ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  7. Offline

    sebagius7110

    Umm Well inside toggleVanish it accepts one player, where I am running the toggleVanish is in an on command so after I check if the sender is an instanceof a Player I do this:

    Code:
    Player p = (Player) sender;
    I can give you the WHOLE class for the command Vanish.java if you really do need it. @amatokus

    Also I do not know why I would need to replace ' with " because Bukit actually generated this Yml from my defaults in the code.

    I could most likely show you all of my code on Saturday but if I get a chance tonight I will try
     
    Last edited: Aug 13, 2015
  8. Offline

    rbrick

  9. Offline

    sebagius7110

    @rbrick

    Main.java:
    Code (open)

    Code:
    package sebagius7110.BetterVanish;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
     
        public static Main main;
    
        @Override
        public void onEnable() {
    
            getCommand("vanish").setExecutor(new Vanish());
    
            loadYamls();
    
        }
    
        private void loadYamls() {
         
            getConfig().addDefault("messages.plugin.vanish.enabled", "Vanish Enabled For: ");
            getConfig().addDefault("messages.plugin.vanish.disabled", "Vanish Disabled For: ");
            getConfig().addDefault("messages.api.vanish.enabled", "Vanish Enabled For: ");
            getConfig().addDefault("messages.api.vanish.disabled", "Vanish Disabled For: ");
    
            getConfig().options().copyDefaults(true);
         
            saveConfig();
    
        }
    
        @Override
        public void onDisable() {
         
        }
    
    }
    


    Instead of using
    Code:
    Main.main; //in Vanish.java
    Should I make a
    Code:
    public static Main main; //In vanish.java and use
    main; //instead of
    Main.main;
     
    Last edited: Aug 13, 2015
  10. Offline

    rbrick

    @sebagius7110 That is what i thought. The reference to Main is null.

    Using static is not recommended, but if you really insist on using static
    Show Spoiler

    Code:
    // This is a terrible practice, which i do not suggest.
    public void onEnable() {
       main = this;
      // rest of on enable here
    }


    Code:
    // Add Main as a argument in your constructor
    private Main mainclass;
    
    // Now we can use it :D
    public Vanish(Main mainPlugin) {
       this.mainclass = mainPlugin;
    }
    Also, it is evident you do not know Java very well. Please learn Java before going into Bukkit. This will save you a lot of trouble in the future.
     
  11. Offline

    sebagius7110

    Wait, so I am having trouble understanding, I add private Main mainclass in what constructor? Vanish.java. Can you give me a full example please. Would I regester /vanish as a command by doing this in Main.java:

    Code:
    getCommand("vanish").setExecutor(new Vanish(this));
    I don't know if I spelt everything right because I am in school right now getting code off the top of my head.
     
  12. Offline

    rbrick

Thread Status:
Not open for further replies.

Share This Page