Solved Null pointer exception i don't understand / loading a world

Discussion in 'Plugin Development' started by mollekake, Oct 11, 2013.

Thread Status:
Not open for further replies.
  1. Hey
    i'm trying to load, and teleport players to a different world.
    I'm using this to create the location from the config:
    Code:java
    1. Vector vector = this.getConfig().getVector("LobbyLoc");
    2. final Location location = new Location(Bukkit.getWorld((String) this.getConfig().get("LobbyWorld")), vector.getX(), vector.getY(), vector.getZ());

    It works fine if i use my normal world "world", but when i use another custom world, it gives me a NPE error, saying that the world for the location is null.
    The x, y and z is correct, but not the world.

    this is the location it creates:
    12:41:27 [INFO] Location{world=null,x=1486.4272658262275,y=5.0,z=-1091.668158407
    991,pitch=0.0,yaw=0.0}

    this is in the config:
    LobbyWorld: worldold
    LobbyLoc:
    ==: Vector
    x: 1486.4272658262275
    y: 5.0
    z: -1091.668158407991

    Halp?
     
  2. Offline

    Craftiii4

    Well, seeing that the world is null, either a) It's not getting the string from the file, or b) the world "worldold" does not exist. I'm not sure if bukkit.getWorld() is case sensitive.
     
  3. a: i tried to output the string in the console, works fine
    b: i don't know, but the casing is correct.
    If i change the .getConfig().get to "MainWorld" which is the normal "world" it works fine.
    Makes no sense to me :S
    Could it be because it's not loaded? kinda would suck because i am trying to load and that is when it fails :p
     
  4. Offline

    lycano

    mollekake the world you teleport to has to be loaded first otherwise you can only access the main world and depending ones like the nether or the end.

    You have two options: 1) check if the world does exist and if its loaded. If its not loaded load it with WorldCreator or use a well known world manager for that like Multiverse.
     
  5. the problem is, i am trying to load it, that is when the error occurs. here is my code:
    Code:java
    1. String world = location.getWorld().getName();
    2. Bukkit.getServer().createWorld(new WorldCreator(world));
    3. instance.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    4. public void run() {
    5. player.teleport(location);
    6. }
    7. },5L);

    using multiverse for just this seems quite overkill.

    i can't load it because the world in location is wrong because i haven't loaded it, and we're in an eternal loop

    if i do it like this:
    Code:java
    1. Bukkit.getServer().createWorld(new WorldCreator(Bukkit.getWorld(this.getConfig().getString("LobbyWorld"))));

    i get an error in eclipse telling me:
    The constructor WorldCreator(World) is undefined.
     
  6. Offline

    lycano

    Check if getConfig().getString("LobbyWorld") returns the correct string.

    Edit:
    In any case Bukkit.getWorld can only access worlds already loaded. Check the WorldCreator constructor it can also accept the worldname itself.
     
  7. The string is returned correctly, the lobbyworld is "worldold" in my testing case.
    I don't want magic values in this plugin, so hardcoding the worldname is not an option :/

    Seems to have solved it, i made the location happen after the world was loaded, loading it like this seems to work:
    Code:java
    1. Bukkit.getServer().createWorld(new WorldCreator(this.getConfig().getString("LobbyWorld")));


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

    lycano

    Which is totally ok i just wanted to make sure that its not null or something. Anyways im not surprised that there is no constructor for WorldCreator(world) ;)

    Checkout the javadocs for WorldCreator.

    new WorldCreator(this.getConfig().getString("LobbyWorld")).copy(Bukkit.getWorld(this.getConfig().getString("LobbyWorld")) can be used.

    You should create the worldCreator object first before passing it to createWorld as you might want to also set the type which is not set during copy.

    1) store LobbyWorld config string into a variable
    2) create WorldCreator object the way you did (this will only create an empty worldcreator with a worldname.
    3) use copy to set the WorldCreator settings like seed, environment, generator
    4) use type to set the worldtype if its different from normal

    See http://jd.bukkit.org/rb/doxygen/d1/d4b/WorldCreator_8java_source.html
     
  9. yeah, but that is if you only want to actually create a new world.
    When you want to load, you just gotta create it. If i'm not completely mistaken.
     
  10. Offline

    lycano

    Try and create a world that has a different type than the default NORMAL. Check if the seed and type matches when using createWorld.
     
  11. Offline

    blablubbabc

    If this world was not created by your plugin, but your plugin is only using it, you could also SoftDepend all the popular WorldManagementPlugins (like Multiverse, MyWorlds, Transporter, etc..), or somehow else make sure, that you are trying to get the world after those management plugins were enabled and have very likly already loaded all the worlds with their settings (another possibility for that would be to delay the loading in your onEnable() for 1 tick, so it will execute after the plugins have started up).

    And only if it is still not loaded, attempt to manual load/create it.

    Or you "dynamically" create your location objects, by only storing the worldnames and coordinates and getting the actual world in the moment you need it. This, by the way, also fixes leaks and other problems if the worlds should be unloaded / reloaded after you have created your location objects.
     
Thread Status:
Not open for further replies.

Share This Page