Solved NullPointerExcpetion at bukkit.getPlayer

Discussion in 'Plugin Development' started by FreeMotion45, Oct 9, 2015.

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



    I tried to fix this error for around 3 ours but whatever I have done didn't fix it.

    So basically this is my code :

                        if((args.length == 2)){
                            if(args[1] == "all"){
                                if(args[2] == "-me"){
                                    Player p = (Player) sender;
                                    World world = p.getWorld();
                                    double x = p.getLocation().getX();
                                    double y = p.getLocation().getY();
                                    double z = p.getLocation().getZ();
                                    float yaw = p.getLocation().getYaw();
                                    float pitch = p.getLocation().getPitch();
                                    for(Player all : Bukkit.getOnlinePlayers()){
                                    Location before = new Location(world, x, y, z, yaw, pitch);
    Now before this code, there are couple of commands which are working fine, but in one of the commands it says NullPointerException for this command. For some reason.

    The error log :
    org.bukkit.command.CommandException: Unhandled exception executing command 'bspawn' in plugin BasicSpawnReloaded v1.0
        at org.bukkit.command.PluginCommand.execute( ~[Bukkitserver.jar:git-Bukkit-efd6cb0]
        at org.bukkit.command.SimpleCommandMap.dispatch( ~[Bukkitserver.jar:git-Bukkit-efd6cb0]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand( ~[Bukkitserver.jar:git-Bukkit-efd6cb0]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand( [Bukkitserver.jar:git-Bukkit-efd6cb0]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a( [Bukkitserver.jar:git-Bukkit-efd6cb0]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [Bukkitserver.jar:git-Bukkit-efd6cb0]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [Bukkitserver.jar:git-Bukkit-efd6cb0]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$ [Bukkitserver.jar:git-Bukkit-efd6cb0]
        at java.util.concurrent.Executors$ Source) [?:1.8.0_60]
        at Source) [?:1.8.0_60]
    The code that is causing the error :

                                Player p2 = Bukkit.getPlayer(args[0]);
                                    p.sendMessage(ChatColor.DARK_RED + "The player is offline or unavailable !");
                                    return false;
    Thanks for your support !
    And please before you comment, don't say : Dude study java, or something like that. Reading those mean comments won't help me.

    Best Regards,

  2. Offline


    We don"t see the cause in your stack trace, it's not the full error..

    #Bukkit.getPlayer() return null if the player is offline.

    The error come from 'p2.isOnline()' if p2 is offline
  3. Offline


    Change your if(p2.isOnline()==false){ to if (p2 == null) { also you should return true, not false, returning false shows your plugin.yml command usage message

    Or you could change Player p2 = Bukkit.getPlayer(args[0]); to offlineplayer, then cast them to player if they're online
  4. Offline


    Thanks for helping.
Thread Status:
Not open for further replies.

Share This Page