title: Bukkit: Player.teleport() throws a NullPointerException
body: <p>I am making a <code>setspawn</code> and <code>spawn</code> command but "<code>/spawn</code>" does not work. I am using Bukkit 1.8.3. Anyone know why? here is the full code:</p>
  
  <h1>Relevant Code</h1>
  
  <h2>/setspawn</h2>
  
  <pre><code>if (cmd.getName().equalsIgnoreCase("setspawn")) {
      if (!sender.hasPermission("setspawn")) {
          sender.sendMessage(ChatColor.RED + "Access denied.");
          return true;
      }
  
      if (!(sender instanceof Player)) {
          sender.sendMessage(ChatColor.RED + "CONSOLE Cannot set spawn!");
          return true;
      }
  
      if (sender instanceof Player) {
          Player player = (Player) sender;
  
          getConfig().set("spawn.world", player.getLocation().getWorld());
          getConfig().set("spawn.x", player.getLocation().getX());
          getConfig().set("spawn.y", player.getLocation().getY());
          getConfig().set("spawn.z", player.getLocation().getZ());
          saveConfig();
          player.sendMessage(ChatColor.GREEN + "Spawn has been set!");
  
          return true;
      }
  }
  </code></pre>
  
  <h2>/spawn</h2>
  
  <pre><code>if (cmd.getName().equalsIgnoreCase("spawn")) {
      if (!sender.hasPermission("spawn")) {
          sender.sendMessage(ChatColor.RED + "Access denied.");
          return true;
      }
  
      if (!(sender instanceof Player)) {
          sender.sendMessage(ChatColor.RED + "CONSOLE Cannot teleport to spawn!");
          return true;
      }
  
      if (getConfig().getConfigurationSection("spawn") == null) {
          Player player = (Player) sender;
          player.sendMessage(ChatColor.RED + "Spawn is not set!");
          return true;
      }
  
      if (sender instanceof Player) {
          Player player = (Player) sender;
          World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
          double x = getConfig().getDouble("spawn.x");
          double y = getConfig().getDouble("spawn.y");
          double z = getConfig().getDouble("spawn.z");
  
          player.teleport(new Location(w, x, y, z));
  
          return true;
      }
  }
  </code></pre>
  
  <hr>
  
  <h1>Full Code</h1>
  
  <pre><code>package me.herobrine112211.ulticmdpack;
  
  import net.milkbowl.vault.economy.Economy;
  import net.milkbowl.vault.economy.EconomyResponse;
  
  import org.bukkit.Bukkit;
  import org.bukkit.ChatColor;
  import org.bukkit.Location;
  import org.bukkit.Material;
  import org.bukkit.World;
  import org.bukkit.command.Command;
  import org.bukkit.command.CommandSender;
  import org.bukkit.entity.Player;
  import org.bukkit.event.EventHandler;
  import org.bukkit.event.Listener;
  import org.bukkit.event.player.PlayerJoinEvent;
  import org.bukkit.inventory.ItemStack;
  import org.bukkit.inventory.PlayerInventory;
  import org.bukkit.plugin.RegisteredServiceProvider;
  import org.bukkit.plugin.java.JavaPlugin;
  
  public class System extends JavaPlugin implements Listener {
      public static Economy econ = null;
  
      public void onEnable() {
          if (!setupEconomy()) {
              getLogger().severe(String.format("[%s] - Disabled because vault was not found!", getDescription().getName()));
              getServer().getPluginManager().disablePlugin(this);
              return;
          }
  
          getConfig().options().copyDefaults(true);
          saveConfig();
          Bukkit.getServer().getPluginManager().registerEvents(this, this);
          Bukkit.getServer().getLogger().info("Ultimate Command Pack Plugin has been actived!");
      }
  
      private boolean setupEconomy() {
          if (getServer().getPluginManager().getPlugin("Vault") == null) {
              return false;
          }
  
          RegisteredServiceProvider &lt; Economy &gt; rsp = getServer().getServicesManager().getRegistration(Economy.class);
  
          if (rsp == null) {
              return false;
          }
  
          econ = rsp.getProvider();
          return econ != null;
      }
  
      public void onDisable() {
          Bukkit.getServer().getLogger().info("Ultimate Command Pack Plugin has been disabled! This will give big problems to server.");
      }
  
      @EventHandler
      public void onPlayerJoin(PlayerJoinEvent joinevent) {
          Player getplayer = joinevent.getPlayer();
  
          getplayer.sendMessage(ChatColor.AQUA + "Hey " + getplayer.getName() + "! Welcome to the Ultimate Prison server!");
  
          if (getplayer.getName().equals("Herobrine112211")) {
              Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.WHITE + "] " + ChatColor.GOLD + "Server Creator Herobrine112211 has joined the game!!!!!!!!!!");
          }
      }
  
      @SuppressWarnings("deprecation")
      public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
          if (cmd.getName().equalsIgnoreCase("contactowner")) {
              if (!sender.hasPermission("contactowner")) {
                  sender.sendMessage(ChatColor.RED + "Access denied.");
                  return true;
              }
  
              if (!(sender instanceof Player)) {
                  sender.sendMessage(ChatColor.RED + "Console will never need owner support.");
                  return true;
              }
  
              if (sender instanceof Player) {
                  Player player = (Player) sender;
                  player.sendMessage(ChatColor.GOLD + "Owner: Herobrine112211. Live chat skype: gangsteris33.");
                  return true;
              }
          }
  
          if (cmd.getName().equalsIgnoreCase("reloadconfig")) {
              if (!sender.hasPermission("config.reload")) {
                  sender.sendMessage(ChatColor.RED + "Access denied.");
                  return true;
              }
  
              reloadConfig();
              sender.sendMessage(ChatColor.RED + "Server config reloaded successfully.");
              return true;
          }
  
          if (cmd.getName().equalsIgnoreCase("saveconfig")) {
              if (!sender.hasPermission("config.save")) {
                  sender.sendMessage(ChatColor.RED + "Access denied.");
                  return true;
              }
  
              reloadConfig();
              sender.sendMessage(ChatColor.RED + "Server config saved successfully.");
              return true;
          }
  
          if (cmd.getName().equalsIgnoreCase("fakeop")) {
              if (!sender.hasPermission("fake.op")) {
                  sender.sendMessage(ChatColor.RED + "Access denied.");
                  return true;
              }
  
              if (args.length == 0) {
                  sender.sendMessage(ChatColor.RED + "Please specify a player!");
                  return true;
              }
  
              Player target = Bukkit.getServer().getPlayer(args[0]);
  
              if (target == null) {
                  sender.sendMessage(ChatColor.RED + "Player must be online!");
                  return true;
              }
  
              target.sendMessage(ChatColor.YELLOW + "You are now op!");
          }
  
          if (cmd.getName().equalsIgnoreCase("fakejoin")) {
              if (!sender.hasPermission("fake.join")) {
                  sender.sendMessage(ChatColor.RED + "Access denied.");
                  return true;
              }
  
              if (args.length == 0) {
                  sender.sendMessage(ChatColor.RED + "Please specify a name!");
                  return true;
              }
  
              Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + args[0] + " joined the game.");
          }
  
          if (cmd.getName().equalsIgnoreCase("broadcast")) {
              if (!sender.hasPermission("broadcast")) {
                  sender.sendMessage(ChatColor.RED + "Access denied.");
                  return true;
              }
  
              if (args.length == 0) {
                  sender.sendMessage(ChatColor.RED + "Please give a message to broadcast!");
                  return true;
              }
  
              StringBuilder strbl = new StringBuilder();
  
              for (int i = 0; i &lt; args.length; i++) {
                  strbl.append(args[i] + " ");
              }
  
              String broadcastas = strbl.toString();
              Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.WHITE + "] " + ChatColor.RED + broadcastas);
          }
  
          if (cmd.getName().equalsIgnoreCase("heal")) {
              if (!sender.hasPermission("heal")) {
                  sender.sendMessage(ChatColor.RED + "Access denied.");
                  return true;
              }
  
              if (!(sender instanceof Player)) {
                  if (args.length == 0) {
                      sender.sendMessage(ChatColor.RED + "Please specify a player!");
                      return true;
                  }
  
                  Player target = Bukkit.getServer().getPlayer(args[0]);
  
                  if (target == null) {
                      sender.sendMessage(ChatColor.RED + "Player must be online to be healed!");
                      return true;
                  }
  
                  target.setHealth(20);
                  target.setFireTicks(0);
                  target.sendMessage(ChatColor.GREEN + "CONSOLE" + " healed you!");
                  sender.sendMessage(ChatColor.GREEN + target.getName() + " is healed!");
  
                  return true;
              }
              if (sender instanceof Player) {
                  if (args.length == 0) {
                      Player player = (Player) sender;
                      player.setHealth(20);
                      player.setFireTicks(0);
                      player.sendMessage(ChatColor.GREEN + "Your health is now full!");
                      return true;
                  }
  
                  Player target = Bukkit.getServer().getPlayer(args[0]);
  
                  if (target == null) {
                      sender.sendMessage(ChatColor.RED + "Player must be online to be healed!");
                      return true;
                  }
  
                  Player player = (Player) sender;
                  target.setHealth(20);
                  target.setFireTicks(0);
                  target.sendMessage(ChatColor.GREEN + player.getName() + " healed you!");
                  player.sendMessage(ChatColor.GREEN + target.getName() + " is healed!");
              }
          }
  
          if (cmd.getName().equalsIgnoreCase("feed")) {
              if (!sender.hasPermission("feed")) {
                  sender.sendMessage(ChatColor.RED + "Access denied.");
                  return true;
              }
              if (!(sender instanceof Player)) {
                  if (args.length == 0) {
                      sender.sendMessage(ChatColor.RED + "Please specify a player!");
                      return true;
                  }
  
                  Player target = Bukkit.getServer().getPlayer(args[0]);
  
                  if (target == null) {
                      sender.sendMessage(ChatColor.RED + "Player must be online to be fed!");
                      return true;
                  }
  
                  target.setHealth(20);
                  target.setFireTicks(0);
                  target.sendMessage(ChatColor.GREEN + "CONSOLE" + " fed you!");
                  sender.sendMessage(ChatColor.GREEN + target.getName() + " hunger is now full!");
  
                  return true;
              }
  
              if (sender instanceof Player) {
                  if (args.length == 0) {
                      Player player = (Player) sender;
                      player.setFoodLevel(20);
                      player.sendMessage(ChatColor.GREEN + "Your hunger is now full!");
  
                      return true;
                  }
  
                  Player target = Bukkit.getServer().getPlayer(args[0]);
  
                  if (target == null) {
                      Player player = (Player) sender;
                      player.sendMessage(ChatColor.RED + "Player must be online to be fed!");
  
                      return true;
                  }
  
                  Player player = (Player) sender;
  
                  target.setFoodLevel(20);
                  target.sendMessage(ChatColor.GREEN + player.getName() + " fed you!");
                  player.sendMessage(ChatColor.GREEN + target.getName() + " hunger is now full!");
              }
          }
  
          if (cmd.getName().equalsIgnoreCase("tpall")) {
              if (!sender.hasPermission("teleport")) {
                  sender.sendMessage(ChatColor.RED + "Access denied.");
                  return true;
              }
  
              if (!(sender instanceof Player)) {
                  sender.sendMessage(ChatColor.RED + "Cannot teleport everyone to console!");
                  return true;
              }
  
              if (sender instanceof Player) {
                  if (args.length == 0) {
                      for (Player allplayers: Bukkit.getOnlinePlayers()) {
                          Player player = (Player) sender;
                          World w = player.getLocation().getWorld();
                          double x = player.getLocation().getX();
                          double y = player.getLocation().getY();
                          double z = player.getLocation().getZ();
  
                          allplayers.teleport(new Location(w, x, y, z));
                          player.sendMessage(ChatColor.GREEN + "All players teleported to you!");
  
                          return true;
                      }
                  }
  
                  for (Player allplayers: Bukkit.getOnlinePlayers()) {
                      Player target = Bukkit.getServer().getPlayer(args[0]);
  
                      if (target == null) {
                          Player player = (Player) sender;
                          player.sendMessage(ChatColor.RED + "Player must be online!");
                          return true;
                      }
  
                      Player player = (Player) sender;
                      World w = target.getLocation().getWorld();
                      double x = target.getLocation().getX();
                      double y = target.getLocation().getY();
                      double z = target.getLocation().getZ();
  
                      allplayers.teleport(new Location(w, x, y, z));
                      player.sendMessage(ChatColor.GREEN + "All players teleported to player!");
  
                      return true;
                  }
              }
          }
  
          if (cmd.getName().equalsIgnoreCase("setspawn")) {
              if (!sender.hasPermission("setspawn")) {
                  sender.sendMessage(ChatColor.RED + "Access denied.");
                  return true;
              }
  
              if (!(sender instanceof Player)) {
                  sender.sendMessage(ChatColor.RED + "CONSOLE Cannot set spawn!");
                  return true;
              }
  
              if (sender instanceof Player) {
                  Player player = (Player) sender;
  
                  getConfig().set("spawn.world", player.getLocation().getWorld());
                  getConfig().set("spawn.x", player.getLocation().getX());
                  getConfig().set("spawn.y", player.getLocation().getY());
                  getConfig().set("spawn.z", player.getLocation().getZ());
                  saveConfig();
                  player.sendMessage(ChatColor.GREEN + "Spawn has been set!");
  
                  return true;
              }
          }
  
          if (cmd.getName().equalsIgnoreCase("spawn")) {
              if (!sender.hasPermission("spawn")) {
                  sender.sendMessage(ChatColor.RED + "Access denied.");
                  return true;
              }
  
              if (!(sender instanceof Player)) {
                  sender.sendMessage(ChatColor.RED + "CONSOLE Cannot teleport to spawn!");
                  return true;
              }
  
              if (getConfig().getConfigurationSection("spawn") == null) {
                  Player player = (Player) sender;
                  player.sendMessage(ChatColor.RED + "Spawn is not set!");
                  return true;
              }
  
              if (sender instanceof Player) {
                  Player player = (Player) sender;
                  World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
                  double x = getConfig().getDouble("spawn.x");
                  double y = getConfig().getDouble("spawn.y");
                  double z = getConfig().getDouble("spawn.z");
  
                  player.teleport(new Location(w, x, y, z));
  
                  return true;
              }
          }
  
          if (cmd.getName().equalsIgnoreCase("kit")) {
              if (!(sender instanceof Player)) {
                  sender.sendMessage(ChatColor.RED + "CONSOLE Cannot get kits!" + ChatColor.RED + "Kits:" + ChatColor.GOLD + "Starter");
                  return true;
              }
  
              if (!sender.hasPermission("kit")) {
                  sender.sendMessage(ChatColor.RED + "Access denied.");
                  return true;
              }
  
              if (sender instanceof Player) {
                  if (args.length == 0) {
                      Player player = (Player) sender;
                      player.sendMessage(ChatColor.RED + "Kits:" + ChatColor.GOLD + "Starter");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("starter")) {
                      if (!sender.hasPermission("kit.starter")) {
                          sender.sendMessage(ChatColor.RED + "Access denied.");
                          return true;
                      }
  
                      if (sender instanceof Player) {
                          Player player = (Player) sender;
                          PlayerInventory pinv = player.getInventory();
                          EconomyResponse withdraw = econ.withdrawPlayer(player.getName(), 10);
  
                           if (withdraw.transactionSuccess()) {
                              pinv.addItem(new ItemStack(Material.DIAMOND_PICKAXE, 1));
                              pinv.addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
                              pinv.addItem(new ItemStack(Material.DIAMOND_AXE, 1));
                              pinv.addItem(new ItemStack(Material.COOKED_BEEF, 20));
                              return true;
                          } else {
                              player.sendMessage(ChatColor.RED + "You dont have required money ammount. Kit purchase failed!");
                              return true;
                          }
                      }
                  }
  
                  Player player = (Player) sender;
                  player.sendMessage(ChatColor.RED + "Kit was not found. Do /kit for kits.");
  
                  return true;
              }
          }
  
          return false;
      }
  }
  </code></pre>
  
  <h2>Error Log</h2>
  
  <pre><code>8:54:05 AM [ERROR] null
  8:54:05 AM org.bukkit.command.CommandException: Unhandled exception executing command 'spawn' in plugin UltimateCommandPack v1.0.0
  8:54:05 AM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:625) ~[bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1077) [bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:937) [bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(SourceFile:37) [bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(SourceFile:9) [bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_75]
  8:54:05 AM at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_75]
  8:54:05 AM at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:670) [bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:336) [bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:626) [bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:534) [bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at java.lang.Thread.run(Unknown Source) [?:1.7.0_75]
  8:54:05 AM Caused by: java.lang.NullPointerException
  8:54:05 AM at org.bukkit.craftbukkit.v1_8_R2.entity.CraftPlayer.teleport(CraftPlayer.java:464) ~[bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity.teleport(CraftEntity.java:223) ~[bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM at me.herobrine112211.ulticmdpack.System.onCommand(System.java:313) ~[?:?]
  8:54:05 AM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[bukkit.jar:git-Bukkit-61ef214]
  8:54:05 AM ... 15 more
  </code></pre>
  
  <p>I have no idea why it does not work. It looks like it should work. If you know the fix show me the solution. Thanks.</p>