Solved java.lang.NullPointerException?

Discussion in 'Spigot Plugin Development' started by XKid1305, Jun 16, 2016.

Thread Status:
Not open for further replies.
  1. hey, im working on a helpop plugin, and i ran into an error.
    here is console:
    Code (Text):
    [20:05:49 INFO]: XKid1305 issued server command: /ahelp hi
    [20:05:49 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ahelp' in plugin AdminHelp v0.2
            at org.bukkit.command.PluginCommand.execute( ~[server.jar:git-Bukkit-0cd0397]
            at org.bukkit.command.SimpleCommandMap.dispatch( ~[server.jar:git-Bukkit-0cd0397]
            at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand( ~[server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.PlayerConnection.handleCommand( [server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.PlayerConnection.a( [server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(SourceFile:37) [server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(SourceFile:9) [server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$ [server.jar:git-Bukkit-0cd0397]
            at java.util.concurrent.Executors$ Source) [?:1.8.0_66]
            at Source) [?:1.8.0_66]
            at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.MinecraftServer.D( [server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.DedicatedServer.D( [server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.MinecraftServer.C( [server.jar:git-Bukkit-0cd0397]
            at [server.jar:git-Bukkit-0cd0397]
            at Source) [?:1.8.0_66]
    Caused by: java.lang.NullPointerException
            at me.xkid1305.Admin.Admin.onCommand( ~[?:?]
            at org.bukkit.command.PluginCommand.execute( ~[server.jar:git-Bukkit-0cd0397]
            ... 15 more
    Code (Text):
    package me.xkid1305.Admin;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class Admin extends JavaPlugin{
        public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
             Player p = (Player) sender;
             if (cmd.getName().equalsIgnoreCase("AHelp")) {
                 if (!(sender instanceof Player)) {
                     sender.sendMessage(ChatColor.DARK_RED + "This command is for player use only.");
                 if (p.hasPermission("")) {
                     if (args.length == 0) {
                         p.sendMessage(ChatColor.DARK_RED + "Please enter a request");
                         return true;
                         p.sendMessage(ChatColor.GREEN + "Help has been requested.");

                         p.sendMessage(ChatColor.RED + "No access");
                Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (target.isOp()) {
                        target.sendMessage(ChatColor.GREEN + "Adminhelp: " + ChatColor.RESET + ChatColor.AQUA + args[0]);
            return false;
    and plugin.yml:
    Code (Text):

    name: AdminHelp
    main: me.xkid1305.Admin.Admin
    version: 0.2
       description: send a message to all of the ops online.
       aliases: [/adminhelp]
       usage: /AHelp <message>

             description: all perms
             default: op
             description: /AHelp <message>
             default: true
    i cant seem to find out what is wrong.
  2. it returns null because the player it's looking for isn't on the server.
    #2 Lumiere, Jun 16, 2016
    Last edited: Jun 16, 2016
    • Agree Agree x 1
  3. Alternatively you could just add a check for:
    Code (Text):
    if (target != null) {
    //Code here
    } else {
    p.sendMessage("The specified target is not online");
  4. Spoon feeding people does nothing to help them at all. They learn nothing and will still be unable to figure out what's wrong the next time they get the issue. There was a reason why I directed them to where they can learn about the issue they're facing and how to handle it.
    • Like Like x 1
  5. always try catch null or check null to prevent this error not telling you this is for all conditions.
  6. thanks guys, i fixed it!
Thread Status:
Not open for further replies.