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(PluginCommand.java:46) ~[server.jar:git-Bukkit-0cd0397]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[server.jar:git-Bukkit-0cd0397]
            at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServer.java:625) ~[server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.PlayerConnection.handleCommand(PlayerConnection.java:1297) [server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1157) [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$1.run(SourceFile:13) [server.jar:git-Bukkit-0cd0397]
            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_9_R2.SystemUtils.a(SourceFile:45) [server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:683) [server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:360) [server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:639) [server.jar:git-Bukkit-0cd0397]
            at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:543) [server.jar:git-Bukkit-0cd0397]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
    Caused by: java.lang.NullPointerException
            at me.xkid1305.Admin.Admin.onCommand(Admin.java:36) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[server.jar:git-Bukkit-0cd0397]
            ... 15 more
    class:
    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;
    import org.bukkit.plugin.java.JavaPlugin;

    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("admin.help")) {
                     if (args.length == 0) {
                         p.sendMessage(ChatColor.DARK_RED + "Please enter a request");
                         return true;
                 
                     }else{
                         p.sendMessage(ChatColor.GREEN + "Help has been requested.");
                     }
                 



                 
                     }else{
                         p.sendMessage(ChatColor.RED + "No access");
                     }
               
                 @SuppressWarnings("deprecation")
                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
    commands:
      AHelp:
       description: send a message to all of the ops online.
       aliases: [/adminhelp]
       usage: /AHelp <message>


    permissions:
     admin.*:
             description: all perms
             default: op
             children:
               admin.help: true
     admin.help:
             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.