Solved NullPointerException

Discussion in 'Plugin Development' started by CodePlaysMinecraft, Jan 23, 2015.

Thread Status:
Not open for further replies.
  1. Hi. For the past month I've been making a Hub plugin for a server, and I'm getting a NullPointerException when I'm calling for an offline player (e.g. /emeralds give Bob 100). The command works fine when done on an online player. I don't get why it's not working. I'm checking if the target is online, and if not tell the sender that the player isn't online.

    Error:
    Code:
    [19:51:37] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'emeralds' in plugin TheRealmHub v2.0.5 BETA
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:703) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:955) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    Caused by: java.lang.NullPointerException
        at me.CodePlaysMC.TheRealmHub.Commands.Emeralds.onCommand(Emeralds.java:69) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        ... 13 more
    Class:
    Code:
    package me.CodePlaysMC.TheRealmHub.Commands;
    
    import me.CodePlaysMC.TheRealmHub.Economy.Methods;
    import me.CodePlaysMC.TheRealmHub.Scoreboard.Scoreboard;
    import me.CodePlaysMC.TheRealmHub.Utilities.ChatUtilities;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Emeralds implements CommandExecutor {
    
        public boolean isInt(String str) {
            try {
                Integer.parseInt(str);
            } catch (NumberFormatException nfe) {
                return false;
            }
            return true;
        }
    
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                Methods m = new Methods(player.getUniqueId());
                if (cmd.getName().equalsIgnoreCase("emeralds")) {
                    if (args.length == 0) {
                        ChatUtilities.sendMessage(player,
                                "You have " + m.getEmeralds() + " emeralds.");
                    } else {
                        if (args.length == 1) {
                            player.sendMessage(ChatColor.RED
                                    + "/emeralds <arguments> <player> <integer>");
                        } else if (args.length == 2) {
                            player.sendMessage(ChatColor.RED
                                    + "/emeralds <arguments> <player> <integer>");
                        } else if (args.length == 3) {
                            if (args[0].equalsIgnoreCase("give")) {
                                if (player.hasPermission("therealm.economy.give")) {
                                    Player target = Bukkit.getPlayer(args[1]);
                                    if (target.isOnline()) {
                                        if (isInt(args[2])) {
                                            int amount = Integer.parseInt(args[2]);
                                            m.giveEmeralds(amount);
                                            ChatUtilities.sendMessage(player,
                                                    args[2] + " emeralds given to "
                                                            + args[1] + ".");
                                            m.saveConfig();
                                            Scoreboard.updateScoreboard(target);
                                        } else {
                                            ChatUtilities
                                                    .sendMessage(player, args[2]
                                                            + " is not an integer!");
                                        }
                                    } else {
                                        ChatUtilities.sendMessage(player,
                                                "The player " + args[1]
                                                        + " is not online.");
                                    }
                                } else {
                                    ChatUtilities
                                            .sendMessage(player,
                                                    "You are not allowed to do this command.");
                                }
                            } else if (args[0].equalsIgnoreCase("set")) {
                                if (player.hasPermission("therealm.economy.set")) {
                                    Player target = Bukkit.getPlayer(args[1]);
                                    if (!target.isOnline()) {
                                        ChatUtilities.sendMessage(player,
                                                "The player " + args[1]
                                                        + " is not online.");
                                    } else {
                                        if (isInt(args[2])) {
                                            int amount = Integer.parseInt(args[2]);
                                            m.setEmeralds(amount);
                                            ChatUtilities.sendMessage(player,
                                                    args[1] + "'s emeralds set to "
                                                            + args[2] + ".");
                                            m.saveConfig();
                                            Scoreboard.updateScoreboard(target);
                                        } else {
                                            ChatUtilities
                                                    .sendMessage(player, args[2]
                                                            + " is not an integer!");
                                        }
                                    }
                                } else {
                                    ChatUtilities
                                            .sendMessage(player,
                                                    "You are not allowed to do this command.");
                                }
                            } else if (args[0].equalsIgnoreCase("take")) {
                                if(player.hasPermission("therealm.economy.take")) {
                                    Player target = Bukkit.getPlayer(args[1]);
                                    if(!target.isOnline()) {
                                        ChatUtilities.sendMessage(player,
                                                "The player " + args[1]
                                                        + " is not online.");
                                    }
                                } else {
                                    ChatUtilities
                                    .sendMessage(player,
                                            "You are not allowed to do this command.");
                                }
                            }
                        }
                    }
                }
            } else {
                sender.sendMessage(ChatColor.DARK_BLUE + "" + ChatColor.BOLD
                        + "The Realm" + ChatColor.GRAY + " >> " + ChatColor.WHITE
                        + "Only players are allowed to do this command.");
            }
            return true;
        }
    }
    Any help is appreciated!
    Thanks in advance. :)
     
  2. Offline

    Ambamore2000

    Simple problem; Nothing to be ashamed of.
    Here is your problem:

    Code:
    Player target = Bukkit.getPlayer(args[1]);
                                    if (target.isOnline()) {
    Now, it says target is null when the player is on. Why is that? Because you are using Bukkit.getPlayer(args[1]), but the player isn't online, so it hasn't been set to anything.

    You're now asking, how do I fix this then? Well, simple enough. Instead of using target.isOnline(), check simply if the player is not null, else, if is null, send the error message to notify the sender that the player isn't online.
     
    CodePlaysMinecraft likes this.
  3. @Ambamore2000 Thanks! It's the simple things that really get you.:)
     
Thread Status:
Not open for further replies.

Share This Page