Solved NullPointerExcpetion at bukkit.getPlayer

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

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

    FreeMotion45

    Hello,

    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 :

    Code:
    if(label.equalsIgnoreCase("bspawn")){
                    if(sender.hasPermission("bsr.spawnall.me")){
                        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()){
                                        all.teleport(p.getWorld().getSpawnLocation());
                                    }
                                    Location before = new Location(world, x, y, z, yaw, pitch);
                                    p.teleport(before);
                                }
                            }
                        }
                    }
                }
    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 :
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'bspawn' in plugin BasicSpawnReloaded v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Bukkitserver.jar:git-Bukkit-efd6cb0]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[Bukkitserver.jar:git-Bukkit-efd6cb0]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[Bukkitserver.jar:git-Bukkit-efd6cb0]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [Bukkitserver.jar:git-Bukkit-efd6cb0]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [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$1.run(SourceFile:13) [Bukkitserver.jar:git-Bukkit-efd6cb0]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_60]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_60]
    
    The code that is causing the error :

    Code:
    @SuppressWarnings("deprecation")
                                Player p2 = Bukkit.getPlayer(args[0]);
                                if(p2.isOnline()==false){
                                    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,

    FreeMotion45.
     
  2. Offline

    caderape

    @FreeMotion45
    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

    Xerox262

    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

    FreeMotion45

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

Share This Page