Solved Check if user is Null - NPE Error

Discussion in 'Plugin Development' started by ReeseNator, Dec 14, 2015.

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

    ReeseNator

    Hi everyone, I am getting a strange NPE error, I've tried so many things, and still I am getting no where. I am typing "/coin bal <a null username>" Im doing that on purpose to check to see if it will catch it, while it does, it won't print the "Could not find player", rather it will give me an NPE.

    The snippet of code that is troubling me is as follows:

    Code:
     if (args.length == 2) {
                Player target = Bukkit.getServer().getPlayer(args[1]);
                String stringTarget = target.getName();
                if (stringTarget != null) {
                sender.sendMessage(ChatColor.GREEN + "Player " + stringTarget + " has a current token balance of: " + ChatColor.RED + Main.econ.getBalance(target.getName()) + " Token(s)");
                }
            } else {
                sender.sendMessage(ChatColor.RED + "Could not find player");
            }
    The error is as follows:

    Code:
    [19:19:46 INFO]: ReeseNator issued server command: /token bal s
    [19:19:46 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'token' in plugin Bytetoken v0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_66]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_66]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
    Caused by: java.lang.NullPointerException
            at net.reesenator.Bytetoken.commands.Bytetoken.onCommand(Bytetoken.java:3) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
    Please help me fix this!

    ~ Reese
     
  2. Offline

    Zombie_Striker

    What is on line 3?
     
  3. Offline

    xMakerx

    The problem is that if that player doesn't exist, target will be null and you're trying to use the getName method on a null object. You need to check if the target is null or surround that part (line 3) with a try catch for a NullPointerException.
     
  4. Offline

    ReeseNator

    I just made it easier to reference, it's line 3 on the snippet.

    Could you provide me with an example so I have some reference?

    ~ Reese
     
  5. Offline

    mythbusterma

    Zombie_Striker likes this.
  6. Offline

    Lightspeed

    Or you can do
    Code:
    if (args.lenght == 2) {
      Player target = Bukkit.getServer().getPlayer(args[1]);
      if (target != null) {
        //doStuff
        return true;
      }
    sender.sendMessage("FAILURE!");
    This will check if the args lenth is 2 and the player is availible if not send a message to sender.
    I know spoonfeeding is bad but, if you gotta see or learn the code sometime I also did a small explanation.
     
  7. Offline

    Zombie_Striker

    "Hey, you need to learn how to write your own code, so I'll make sure you don't have to!". You should never have to have the code in order to know how to make it.

    BTW, If you cant explain what to do without showing code, either you don't know what you're doing or are too lazy to make sure the other person knows what he's doing.
     
  8. Offline

    ReeseNator

    Guys, I fixed it!

    For anyone else who was in my situation, here is how to do it:

    Code:
    if (args.length == 2) {
                Player target = Bukkit.getServer().getPlayer(args[1]);
                if (target != null) {
                    String stringTarget = target.getName(); //Get the name AFTER you confirm the player isnt null
                    sender.sendMessage(plugin.getFormat() + ChatColor.GREEN + "Player " + stringTarget + " has a current token balance of: " + ChatColor.RED + Main.econ.getBalance(target.getName()) + " Token(s)");
                } else {
                    sender.sendMessage(plugin.getFormat() + ChatColor.RED + "Could not find player: " + args[1]);
                }
            }
    Also, if you guys don't mind, I have one last question in regards to vault. Is there a way to save a deposited amount to a config file? Here is where I am stuck:

    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            if (Bukkit.getServer().getOnlinePlayers().size() == 1) {
                Player target = event.getPlayer();
                String targetName = target.getName();
                econ.depositPlayer(target.getName(), 100); //<-- How do I save that in my config?
                target.sendMessage("You have 100 tokens");
            }
        }
    Hope you all can help me some more! I'd also like to thank you all who have already helped!

    ~ Reese
     
  9. Offline

    Zombie_Striker

    @ReeseNator
    Mark thread as solved if your problem has been solved.
     
  10. Offline

    ReeseNator

    Alright
     
  11. Offline

    xMakerx

    It's an option and that isn't incorrect code. There's multiple ways to do things in programming and you should know that, @mythbusterma.
     
  12. Offline

    mythbusterma

    @xMakerx

    And some of them are wrong. For example, catching RuntimeExceptions. There is never a reason to do it.
     
  13. And using statics to share changing/temporary variables!
     
    mythbusterma likes this.
  14. Offline

    xMakerx

    Doesn't make them wrong. Yes, it is bad practice, I just said you could do either one. There's a difference between being wrong and having bad practice. Bad practice is when something isn't recommended and wrong is when something won't work, at least to my understanding.
     
Thread Status:
Not open for further replies.

Share This Page