How to check if an argument = something in a config.

Discussion in 'Plugin Development' started by kayc01, Oct 29, 2015.

Thread Status:
Not open for further replies.
  1. Hey, i am trying to check if args[1] in my command equals gym1 to gym32.
    Anything else it needs to show a message in chat saying "That is not a valid gym".

    I tried:


    Code:
     for (int i = 1; i <= 32; i++) {
         if (!args[1].equalsIgnoreCase("gym"+i)) {
                  p.sendMessage("please type a valid gym")
        else {
           //continue with the code.
    }
    }
    However, it repeated the error 32 times and no matter what was put in args 1, it showed that error.

    I also tried just check if args[1] existed in the config (all my gym's exist under "config.gym#" # = a number between 1 and 32. (example: config.gym1)

    However it gave me this error:


    Code:
    [04:20:35 INFO]: ABkayCkay issued server command: /gym close gfgf
    [04:20:35 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'gym' in plugin PixelmonGym v4.2
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:742) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
    Caused by: java.lang.NullPointerException
            at me.Ckay.gym.PixelGym.onCommand(PixelGym.java:915) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            ... 13 more
    The line i used to check if the config args were null in the config was:


    Code:
    if (getConfig().getString("config."+args[1]).equals(null)) {
                                  p.sendMessage(ChatColor.RED + "The gym " +args[1]+ " does not exist!");
    }
    Any help is appreciated.
     
  2. Offline

    567legodude

    @kayc01 There are a couple of ways. One could be to get the number from the string.
    To do that, use a regex and the replaceAll method.
    The regex would be "[^0-9]" and replace with "" and you are left with just the number.
    Then parse that number and check if it's in the range, and then continue to grab from the config.
     
  3. Offline

    Scimiguy

    Can you show us your whole PixelGym class file?
    Helps us use that error
     
  4. That error caused on line 915 is:

    Code:
    if (getConfig().getString("config."+args[1]).equals(null)) {
    I no longer have it in my code as it was causing an error. But here is an another example of it:


    Code:
    if ((args[0].equalsIgnoreCase("rules"))) {
                             
                              if (getConfig().getString("config."+args[1]).equals(null)) {
                                  p.sendMessage(ChatColor.RED + "The gym " +args[1]+ " does not exist!");
                                  p.sendMessage(ChatColor.RED + "Try /gym rules gym1");
                              }
                              else {
                              if (getConfig().getString("config."+args[1]+"enabled").equalsIgnoreCase("true")) {
                                  p.sendMessage(ChatColor.GOLD + "----- "+getConfig().getString("config."+args[1])+" Gym Rules -----");
                                  p.sendMessage("");
                                  p.sendMessage(ChatColor.GREEN + "1) " + getConfig().getString("config."+args[1]+"rule1"));
                                  p.sendMessage(ChatColor.GREEN + "2) " + getConfig().getString("config."+args[1]+"rule2"));
                                  p.sendMessage(ChatColor.GREEN + "3) " + getConfig().getString("config."+args[1]+"rule3"));
                                  p.sendMessage(ChatColor.GREEN + "4) " + getConfig().getString("config."+args[1]+"rule4"));
                                  p.sendMessage(ChatColor.GREEN + "5) " + getConfig().getString("config."+args[1]+"rule5"));
                               }
                              }
                       }
    I don't know what is wrong with that null check?
     
  5. Offline

    Scimiguy

    Well if you got rid of it, what's the error now?
     
  6. Well then it won't check if it is null, so if it is null it will just throw a Null error?


    Code:
    [12:00:02 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'gym' in plugin PixelmonGym v4.2
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:742) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
    Caused by: java.lang.NullPointerException
            at me.Ckay.gym.PixelGym.onCommand(PixelGym.java:920) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            ... 13 more
     
  7. Offline

    Scimiguy

    @kayc01
    So then why did you remove it? lol

    Give us your full class, and the error that results from using that compiled class.
    We need the full class to match the code to the error, there's no point changing it on us.
     
  8. Okay, so i used it on /gym open gym# as well, (line 777)


    Code:
    [12:10:00 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'gym' in plugin PixelmonGym v4.2
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:742) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
    Caused by: java.lang.NullPointerException
            at me.Ckay.gym.PixelGym.onCommand(PixelGym.java:777) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            ... 13 more
    whole class, as you wanted... its a big one. Lol.

    It does not allow me to post here, so pastebin it is..
    http://pastebin.com/TCpdtq5F

    Just head to line 777 i guess :p
     
  9. Offline

    Scimiguy

    @kayc01
    Don't do it like that, use config.contains() instead
     
  10. Like this?


    Code:
    if (!config.contains("config."+args[1])) {
                                  p.sendMessage(ChatColor.RED + "Please open a valid Gym! " + args[1] + " doesnt exist in the config.");
                              }
                              else {
    }
    EDIT:
    Still getting this error, have i done it wrong?


    Code:
    [12:35:17 INFO]: ABkayCkay issued server command: /gym open gfg
    [12:35:17 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'gym' in plugin PixelmonGym v4.2
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:742) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
    Caused by: java.lang.NullPointerException
            at me.Ckay.gym.PixelGym.onCommand(PixelGym.java:777) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            ... 13 more
     
  11. Offline

    Maxx_Qc

    @kayc01 Stop sending logs if you don't send the line that cause these errors
     
  12. When i have.. if you read...

    They are both line 777, no point re-posting my whole class when i only changed

    Code:
    if (getConfig().getString("config."+args[1]).equals(null)) {
    to

    Code:
    if (!config.contains("config."+args[1])) {
    so you know it is that, that is causing the error xD
     
  13. @kayc01
    Please dude, just try...
    NullPointerException
    Code:
    if (!config.contains("config."+args[1])) {
    Only one thing can throw an NPE here...
     
  14. The ("config."+args[1]), but i am trying to check if it is null. And stop the internal error.
    Not have an internal error even when this is supposedly checking if it is null.
    Unless i need to break from the if if it is null.
     
  15. Offline

    grrocks

    OP not helping with the original problem but just use for statements for stuff like line 124 to 155 and a few other stuff :p
     
  16. I have used For's where ever i could think.
    The stuff that includes setting a score does not want to have anything else in it.

    Wont accept stuff like

    this.setScore(i);

    etc...
     
  17. Offline

    Scimiguy

    @kayc01
    For the sake of continuity, can you put a new Pastebin with your updated code?
    You did change more than what was specified, so it's worth showing us where it's at right now
     
  18. Yeah sure, sorry.
    However i have removed the check if null for now on one of the commands, it is on line 1098 though.
    http://pastebin.com/G31SVWPC

    Seems to format weirdly in paste bin but hope it is readable.

    Forgot the shortcut to format code in eclipse so it is a little easier to read.
    Either how it looks on paste bin is not how it looks in eclipse.

    Just a double check that you know what i am trying to do.

    In my config i have

    config:
    gym1: 'Rock'
    gym2: 'Grass'

    etc...

    I want to check if gym1 is null (does not exist in the config), not that nothing is in the ''s.
    So i don't want to check if Rock is null, i want to check if gym1 is null. (position wise)

    So basically if a user types the command /gym open gym1, that would work because "gym1:"
    exists. However if they did /gym open gyml5we (random name or typo), it would tell them that, that gym is not in the config.

    I hope this makes sense.

    Thanks!
     
  19. Offline

    Scimiguy

    I'm assuming we're looking at line 945 now?

    use equals(""); then, since it returns an empty string if gym1: is in the config but is blank.
     
  20. Not sure if that is what i want..
    by sounds of it it is checking if something that exists in the config's value is blank, not if something exist's in the value at all?

    Or may have just been the way you worded it?

    But you are implying:

    Code:
    if (getConfig().getString("config."+args[1]).equals("")) {
    will work?
     
  21. @kayc01
    null checks are to see if objects are null to prevent NullPointerExceptions from being called when calling methods on them. When you use Object#equals(null) you perform a method on it, so it doesn't prevent NPEs at all.
     
  22. @megamichiel @Scimiguy

    if(getConfig().getString("config."+args[1]).equals("")){

    Still returns NPE :/
     
  23. Offline

    tkuiyeager1

    @kayc01 what is not working with getConfig().contains("string");?
     
  24. @kayc01
    Has noone ever taught you about == ?
     
  25. All i know is that it mean equals/is. Or isn't if a ! is used.

    Might just do:

    If (args1.equalsIgnoreCase("gym1") || (args1.equalsIgnoreCase("gym2"), etc.... {
    //do rest of code,
    else {
    p.sendMessage(ChatColor.RED + "Please open a valid gym" xD

    Kind of tedious but it would work...

    They are seem to give me NPE, because it is null. But surely a null check is to check if something is null, not give me an error when checking if something thats null is null...
     
  26. @kayc01
    Since you can't perform methods on it, the only way to check if it's not null is by doing Object != null...
     
  27. Offline

    DoggyCode™

    Your checking it the value is equal to null. Using "==null" would rather check if it exists.
     
  28. So like: ?


    Code:
    if (getConfig().getString("config."+args[1]) != (null)) {
                                  p.sendMessage(ChatColor.RED + "The gym " +args[1]+ " does not exist!");
                                  p.sendMessage(ChatColor.RED + "Try /gym close gym1");
                              }
                              else {
     
  29. Offline

    mythbusterma

Thread Status:
Not open for further replies.

Share This Page