Issue with for loop setting sections to static variables.

Discussion in 'Plugin Development' started by Tim4209, Jun 27, 2013.

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

    Tim4209

    Does anyone know why this don't work?
    when i try to use them i get a null pointer exception
    and also is there a way to set these without doing if name.equals
    {static} = new Location(world, PosX, PosY, PosZ, Yaw, Pitch);


    public static Location SpawnPosition;
    public static Location ShopPosition;

    Code:
            try {
                Set<String> cs = config.getConfigurationSection("Warps").getKeys(false);
                if (cs != null){
                    for (String sub : cs){
                        String name = sub;
                        World world = Bukkit.getServer().getWorld("Warps." + name + ".World");
                        double PosX = config.getDouble("Warps." + name + ".X");
                        double PosY = config.getDouble("Warps." + name + ".Y");
                        double PosZ = config.getDouble("Warps." + name + ".Z");
                        float Pitch = (float) config.getDouble("Warps." + name + "Pitch");
                        float Yaw = (float) config.getDouble("Warps." + name + "Yaw");
                        if(name.equalsIgnoreCase("ShopPoint")) {
                            ShopPosition = new Location(world, PosX, PosY, PosZ, Yaw, Pitch);
                        }
                        if(name.equalsIgnoreCase("SpawnPoint")) {
                            SpawnPosition = new Location(world, PosX, PosY, PosZ, Yaw, Pitch);
                        }
                    }
                }     
            } catch (Exception e) {
                e.printStackTrace();
            }
     
  2. Offline

    LuigiPenguin

    I think the most reason why people ditched this question 5/6 of this forums dont know how to code xD
     
  3. Offline

    TnT

    Moved to the correct forum.

    Point them to a helpful tutorial. Clearly they want to learn if they are here asking for assistance with coding.
     
    Ivan likes this.
  4. Offline

    Tim4209

    If i take the loop out and set them individually it works fine.
    the null pointer happens when i actually try to teleport to spawnposition or shopposition.
    but i can set them just fine the same way without looping it and it works.
    i just wanted to do a loop for less coding and easier to add more stuff in the future.

    solved it myself, forgot to get the world from the config.
    Bukkit.getServer().getWorld(config.getString("Warps." + name + ".World"));

    cant belive i missed that little mistake.. lol thanks for the help guys
    and i fixed that if thing by using a hashmap

    locations.put(name,location);

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

Share This Page