Command giving an error

Discussion in 'Plugin Development' started by Doubtstand, Aug 12, 2015.

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

    Doubtstand

    I use this class to set a player's rank stored in a HashMap<UUID, String>.
    Code:java
    1.  
    2. public class RankCommand implements CommandExecutor{
    3.  
    4. Main plugin;
    5.  
    6. public RankCommand(Main instance){
    7. plugin = instance;
    8. }
    9.  
    10. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    11. if(cmd.getName().equalsIgnoreCase("setrank")){
    12. if(args.length == 0){
    13. sender.sendMessage(ChatColor.GRAY + "Please use " + ChatColor.YELLOW + "/setrank <player> <rank>" + ChatColor.GRAY + ".");
    14. return true;
    15. }
    16. if(args.length == 1){
    17. sender.sendMessage(ChatColor.GRAY + "Please specify a " + ChatColor.YELLOW + "Rank" + ChatColor.GRAY + ".");
    18. return true;
    19. }
    20. if(args.length == 2){
    21.  
    22. Player target = Bukkit.getServer().getPlayer(args[0]);
    23.  
    24. if(target == null){
    25. sender.sendMessage(ChatColor.GRAY + "Could not find the player " + ChatColor.YELLOW + args[0]);
    26. return true;
    27. }
    28.  
    29. if(args[1].equalsIgnoreCase("user")){
    30. plugin.rank.put(target.getUniqueId(), "user");
    31. sender.sendMessage(ChatColor.GRAY + "Player " + ChatColor.YELLOW + target.getName() + ChatColor.GRAY + "is now an " + ChatColor.YELLOW + "user" + ChatColor.GRAY + ".");
    32. return true;
    33. }
    34.  
    35. if(args[1].equalsIgnoreCase("admin")){
    36. plugin.rank.put(target.getUniqueId(), "admin");
    37. sender.sendMessage(ChatColor.GRAY + "Player " + ChatColor.YELLOW + target.getName() + ChatColor.GRAY + "is now an " + ChatColor.YELLOW + "admin" + ChatColor.GRAY + ".");
    38. return true;
    39. }
    40. }
    41. if(args.length > 2){
    42. sender.sendMessage(ChatColor.GRAY + "Please use " + ChatColor.YELLOW + "/setrank <player> <rank>" + ChatColor.GRAY + ".");
    43. return true;
    44. }
    45. }
    46. return true;
    47. }
    48. }
    49.  
    50.  


    it worked until I tried to do /setrank doubtstand user and it gave me an error:

    Code:java
    1.  
    2. 00:00:38 [ERROR] null
    3. 00:00:38 org.bukkit.command.CommandException: Unhandled exception executing command 'setrank' in plugin DevTest v1.0
    4. 00:00:38 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-044d928-e8c6403]
    5. 00:00:38 at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-044d928-e8c6403]
    6. 00:00:38 at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:642) ~[spigot.jar:git-Spigot-044d928-e8c6403]
    7. 00:00:38 at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1135) [spigot.jar:git-Spigot-044d928-e8c6403]
    8. 00:00:38 at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:970) [spigot.jar:git-Spigot-044d928-e8c6403]
    9. 00:00:38 at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-044d928-e8c6403]
    10. 00:00:38 at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-044d928-e8c6403]
    11. 00:00:38 at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-044d928-e8c6403]
    12. 00:00:38 at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_51]
    13. 00:00:38 at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_51]
    14. 00:00:38 at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot.jar:git-Spigot-044d928-e8c6403]
    15. 00:00:38 at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:718) [spigot.jar:git-Spigot-044d928-e8c6403]
    16. 00:00:38 at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot.jar:git-Spigot-044d928-e8c6403]
    17. 00:00:38 at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-044d928-e8c6403]
    18. 00:00:38 at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-044d928-e8c6403]
    19. 00:00:38 at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    20. 00:00:38 Caused by: java.lang.NullPointerException
    21. 00:00:38 at commands.RankCommand.onCommand(RankCommand.java:40) ~[?:?]
    22. 00:00:38 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-044d928-e8c6403]
    23. 00:00:38 ... 15 more
    24.  


    So it gives an error when adding "user" to the hashmap, but how do I fix this?

    Thanks, Sten.
     
  2. Offline

    mythbusterma

    @Doubtstand

    Something on line 40 is null, as per:

    I'm willing to bet it's "rank," and that really shouldn't be a public member anyway.
     
  3. Offline

    mine-care

    Also please enhance your code's efficiency by using if ,else if, else statements, See
    args.length cannot be both 0, 1 and 2 so here an if else if statement would be good.
     
  4. Offline

    finalblade1234

    Plugin main; rank (might) be null, is it possible you could post your main class?
     
    Doubtstand likes this.
  5. Offline

    Doubtstand

    That was the case, fixed it by now but thanks anyways ;)
     
    finalblade1234 likes this.
Thread Status:
Not open for further replies.

Share This Page