Can't get Loading Locations from Config Working.

Discussion in 'Plugin Development' started by nxtguy, May 11, 2013.

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

    nxtguy

    I can properly save location information to the config. But I'm having troubles reading them and teleporting the players.

    Here is my config example.
    Code:
    locations:
      guards:
        X: 291.49520758332517
        Y: 81.0
        Z: 205.56669250532067
        Yaw: -313.65103
        Pitch: 1.6499916
    I have a List<String> with all the players that I want to teleport. When running the method I'm getting a java.lang.NullPointerException error in the console.

    Here is the method:
    Code:
    @SuppressWarnings("null")
    public void teleportGuards() {
        int index = 0;
       
        //Get location from configuration file.
        Location newloc = null;
        String stringYaw = getConfig().getString("location.guards.Yaw");
        String stringPitch = getConfig().getString("location.guards.Pitch");
       
        String stringX = getConfig().getString("location.guards.X");
        String stringY = getConfig().getString("location.guards.Y");
        String stringZ = getConfig().getString("location.guards.Z");
       
        float yaw = Float.parseFloat(stringYaw);
        float pitch = Float.parseFloat(stringPitch);
       
        float X = Float.parseFloat(stringX);
        float Y = Float.parseFloat(stringY);
        float Z = Float.parseFloat(stringZ);
       
        newloc.setYaw(yaw);
        newloc.setPitch(pitch);
       
        newloc.setX(X);
        newloc.setY(Y);
        newloc.setZ(Z);
       
        //Now loop through all players and teleport them there.
        while (guards.size() > index) {
            Player player = Bukkit.getPlayer(guards.get(index));
            player.teleport(newloc);
           
            index++;
        }
       
    }
    Does anyone have any ideas on how to get this working?
     
  2. Offline

    catageek

    Just replace the line:
    Code:
    Location newloc = null;
    by
    Code:
    Location newloc = new Location();
     
  3. Offline

    nxtguy

    When I do that, I get the error
    Code:
    The constructor Location() is undefined
    in Eclipse.
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    You get that error is because location does not have an no args constructor defined...
     
  5. Offline

    nxtguy

    Sagacious_Zed If I need to define the location right when I initialize the location variable, then when would I ever use loc.setX(); ?
     
  6. Offline

    catageek

    Sorry about my mistake, you must use the constructor to create your Location object with proper variables.

    the constructor is Location (final World world, final double x, final double y, final double z, final float yaw, final float pitch)

    you never have to use setX()
     
  7. Offline

    nxtguy

    catageek Sagacious_Zed I added the variables right away when creating the newloc variable however I'm still getting the error

    EDIT: Sorry this error now:
    Code:
    2013-05-11 13:01:48 [WARNING] [PrisonEscape] Task #2 for PrisonEscape v0.1 generated an exception
    java.lang.NullPointerException
        at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
        at java.lang.Float.parseFloat(Unknown Source)
        at com.nxtguy.me.Main.teleportGuards(Main.java:355)
        at com.nxtguy.me.Main$1.run(Main.java:275)
        at org.bukkit.craftbukkit.v1_5_R3.scheduler.CraftTask.run(CraftTask.java:53)
        at org.bukkit.craftbukkit.v1_5_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
        at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:513)
        at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
        at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
        at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
        at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Here's the new method:
    Code:
    public void teleportGuards() {
        int index = 0;
     
        //Get location from configuration file.
        String stringYaw = getConfig().getString("location.guards.Yaw");
        String stringPitch = getConfig().getString("location.guards.Pitch");
     
        String stringX = getConfig().getString("location.guards.X");
        String stringY = getConfig().getString("location.guards.Y");
        String stringZ = getConfig().getString("location.guards.Z");
     
        final float yaw = Float.parseFloat(stringYaw);
        final float pitch = Float.parseFloat(stringPitch);
     
        final double X = Double.parseDouble(stringX);
        final double Y = Double.parseDouble(stringY);
        final double Z = Double.parseDouble(stringZ);
     
        World world = Bukkit.getWorld("world");
     
        Location newloc = new Location(world, X, Y, Z, yaw, pitch);
     
        //newloc.setYaw(yaw);
        //newloc.setPitch(pitch);
     
        //newloc.setX(X);
        //newloc.setY(Y);
        //newloc.setZ(Z);
     
        //Now loop through all players and teleport them there.
        while (guards.size() > index) {
            Player player = Bukkit.getPlayer(guards.get(index));
            player.teleport(newloc);
         
            index++;
        }
     
    }
    Thanks for helping guys. :D
     
  8. Offline

    catageek

    Please point line #355 of your code where the NullPointerException occurs
     
  9. Offline

    nxtguy

    Code:
    String stringZ = getConfig().getString("location.guards.Z");
    Is line #355
     
  10. Offline

    catageek

  11. Offline

    nxtguy

    I used Bukkit to create an config. (The Location is set by a command.) And I double checked with a YML validator so I don't think that is the problem.

    What is weird is when I first start up the server I get the long error, however when I reload I get the short NullPointerException error.

    I'm running the method from inside a scheduler, would this be a problem?
     
  12. Offline

    Sagacious_Zed Bukkit Docs

    nxtguy That depends on what you did, and what it is programmed to do.
     
  13. Offline

    catageek

    Maybe accessing the config file in a static way would be better from an asynchronous task (is this what you mean by "scheduler ?). get the config file from Bukkit.getPluginManager().getPlugin("...").getConfig() method

    What is weird is that the exception is raised on the 'Z' line, and not before...
     
  14. Offline

    nxtguy

    The scheduler basically checks if there are enough players online, then sorts them in two Lists. List<String> guards being one of them. Once it sorts them in to groups, it sets a variable to false to prevent the scheduler from continuing to run. (It's actually still running, but not getting anywhere.)


    I added Bukkit.getPluginManager().getPlugin("PrisonEscape") however I'm still getting the long error on the first run, and the short error when the code runs after a reload.



    Code:
    public void teleportGuards() {
        int index = 0;
       
        //Get location from configuration file.
        String stringYaw = Bukkit.getPluginManager().getPlugin("PrisonEscape").getConfig().getString("location.guards.Yaw");
        String stringPitch = Bukkit.getPluginManager().getPlugin("PrisonEscape").getConfig().getString("location.guards.Pitch");
       
        String stringX = Bukkit.getPluginManager().getPlugin("PrisonEscape").getConfig().getString("location.guards.X");
        String stringY = Bukkit.getPluginManager().getPlugin("PrisonEscape").getConfig().getString("location.guards.Y");
        String stringZ = Bukkit.getPluginManager().getPlugin("PrisonEscape").getConfig().getString("location.guards.Z");
       
        final float yaw = Float.parseFloat(stringYaw);
        final float pitch = Float.parseFloat(stringPitch);
       
        final double X = Double.parseDouble(stringX);
        final double Y = Double.parseDouble(stringY);
        final double Z = Double.parseDouble(stringZ);
       
        World world = Bukkit.getWorld("world");
       
        Location newloc = new Location(world, X, Y, Z, yaw, pitch);
       
        //newloc.setYaw(yaw);
        //newloc.setPitch(pitch);
       
        //newloc.setX(X);
        //newloc.setY(Y);
        //newloc.setZ(Z);
       
        //Now loop through all players and teleport them there.
        while (guards.size() > index) {
            Player player = Bukkit.getPlayer(guards.get(index));
            player.teleport(newloc);
           
            index++;
        }
       
    }
    When I manually add the location right in the code (not from config) it works fine. :/

    Is the parsing in my code correct?

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

    Sagacious_Zed Bukkit Docs

    In that case, you may want to put in a sanity check that you are getting the values you expect. Otherwise, you may be referencing the wrong instance of certain objects.
     
  16. Offline

    nxtguy

    How would I do that?
     
  17. Offline

    Sagacious_Zed Bukkit Docs

    However you want to, and makes it easy for you to debug. Often times this may be accomplished with logging.
     
  18. Offline

    nxtguy

    Could you give me an example? I have no idea how to check if a variable is the correct type I want, other than defining it at the beginning.
     
  19. Offline

    Sagacious_Zed Bukkit Docs

    You can only declare variables with a type...
     
  20. Offline

    nxtguy

    Problem was I was looking for "location.guards.X" when "locations.guards.X" was written in my config.

    -_-

    Thanks Sagacious_Zed and catageek for your help. :D
     
Thread Status:
Not open for further replies.

Share This Page