Solved getSpawnLocation() throwing NullPointerException?

Discussion in 'Plugin Development' started by Croug, Jul 7, 2013.

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

    Croug

    Exactly what the title says, when I call getSpawnLocation() it throws a null pointer exception, found on line 28 of the provided code snippet.

    warpPlayer() method:
    Code:java
    1. private boolean warpPlayer(CommandSender sender, String[]args){
    2. if(!(sender instanceof Player)){
    3. plugin.getLogger().info("This Is An In Game Command");
    4. return true;
    5. }else if(args.length != 1){
    6. return false;
    7. }
    8. Player player = (Player) sender;
    9. Location loc = null;
    10. World dest = null;
    11. dest = plugin.getServer().getWorld(args[0]);
    12. if(dest == null){
    13. sender.sendMessage("Could Not Find '" + args[0] + "'");
    14. }
    15. if(player.getWorld().getName().equalsIgnoreCase(args[0])){
    16. player.sendMessage("You Are Already On This World!");
    17. return true;
    18. }else{
    19. loc = (Location) plugin.users.get(player.getName() + ".worlds." + args[0]);
    20. if(loc != null){
    21. sender.sendMessage("Warping...");
    22. plugin.users.set(player.getName() + ".worlds." + player.getWorld().getName(), player.getLocation());
    23. player.teleport(loc);
    24. sender.sendMessage("Poof!");
    25. return true;
    26. }
    27. try{
    28. loc = dest.getSpawnLocation();
    29. sender.sendMessage("Warping...");
    30. plugin.users.set(player.getName() + ".worlds." + player.getWorld().getName(), player.getLocation());
    31. player.teleport(loc);
    32. sender.sendMessage("Poof!");
    33. return true;
    34. sender.sendMessage(ChatColor.RED + "Error, check console");
    35. plugin.getLogger().severe(ChatColor.RED + "SEVERE ERROR");
    36. ex.printStackTrace();
    37. return true;
    38. }
    39. }
    40. }


    Come on guys, I hate to pester but I really need this to start working.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  2. Offline

    re4ly

    Code:java
    1. dest = plugin.getServer().getWorld(args[0]);
    2. loc = dest.getSpawnLocation();
     
  3. Offline

    ZeusAllMighty11

    You declared a null world, and never did anything else to prevent the NPE
     
  4. Offline

    Croug

    re4ly that is in fact the code I used
     
  5. Offline

    re4ly

    Code:java
    1. World dest = null;
    2. to ->
    3. World dest;
     
  6. Offline

    Croug

    yes I did, directly below it I used dest = plugin.getServer().getWorld(args[0]);

    re4ly nope

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  7. Offline

    slayr288

    Croug
    Didn't return false after sending the message on the world null check.
     
  8. Offline

    Croug

    slayr288 it returns true so it doesn't send the usage message

    Worlds should be created and loaded with default spawn locations correct, or do I have to set them manually?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  9. Offline

    slayr288

    Croug
    Well you didn't return true or false, so the code keeps going until it returns something, and if it keeps going with a null world, you'll get an NPE.
     
  10. Offline

    Croug

    slayr288 first thing: dest isn't null, right below it I set it to be the world the player specified in the command line.
    second thing: i do return, i return true
     
  11. Offline

    slayr288

    Croug
    Code:
            if(dest == null){
                sender.sendMessage("Could Not Find '" + args[0] + "'");
            }
    You don't return on this check. If the world is null, the method will continue anyways.
    If you ask for help, listen to the people helping you..
     
  12. Offline

    Croug

    slayr288 I see what your saying now, I think that's it! Thank You!

    slayr288 I just didn't realize what you were trying to say.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page