Null pointer exception

Discussion in 'Plugin Development' started by i3ick, May 28, 2013.

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

    i3ick

    Hey, when I run this script I get a null pointer exception, why? my config.yml isn't empty

    Code:
        else if(cmd.getName().equalsIgnoreCase("red")){
                int redspawnX = this.getConfig().getInt("redspawn" + ".X");
                int redspawnY = this.getConfig().getInt("redspawn" + ".Y");
                int redspawnZ = this.getConfig().getInt("redspawn" + ".Z");
                int redspawnYaw = this.getConfig().getInt("redspawn" + ".Yaw");
                int redspawnPitch = this.getConfig().getInt("redspawn" + ".Pitch");
                Object world = this.getConfig().get("redspawn" + ".World");
                 
                    Location redspawn = new Location((World) world, redspawnX, redspawnY, redspawnZ, redspawnYaw, redspawnPitch);
                 
                    player.teleport(redspawn);
               
            }
    Thanks
     
  2. Offline

    Rocoty

    Please post more of your code, and the stacktrace, and provide us with line numbers.
     
  3. Offline

    ZeusAllMighty11

    World objects are not serializable
     
  4. Offline

    Rocoty

    Yeah, the code would also produce a ClassCastException. But still, he was talking about a NullPointerException...so that has to be some place too.
     
  5. Offline

    i3ick

    Ok so this saves the info for red spawn:
    Code:
        else if(cmd.getName().equalsIgnoreCase("ftsetred")){
                this.getConfig().set("redspawn" + ".X", player.getLocation().getBlockX());
                    this.getConfig().set("redspawn" + ".Y", player.getLocation().getBlockY());
                    this.getConfig().set("redspawn" + ".Z", player.getLocation().getBlockZ());
                    this.getConfig().set("redspawn" + ".Yaw", player.getLocation().getYaw());
                    this.getConfig().set("redspawn" + ".Pitch", player.getLocation().getPitch());
                    this.getConfig().set("redspawn" + ".World", player.getLocation().getWorld());
                    this.getConfig().options().copyDefaults(true);
                      this.saveConfig();
                    player.sendMessage(ChatColor.YELLOW + "Red Spawn saved successfully");   
            }
    And these 2 codes are actually all there is. But I figured something is wrong with the one posted first at this one seems to work just fine
     
  6. Offline

    ZeusAllMighty11

    Rocoty

    It would cause his world to be null, also his plugin variable could be null or even the paths in config
     
  7. Offline

    i3ick

  8. i3ick
    It means we're guessing because you haven't posted the error... and when you do post the error you should read the trace after "Caused by" and point us to the line that triggers at.
     
  9. Offline

    i3ick

    Digi
    Oh right, sorry

    line 84 is:

    player.teleport(redspawn);

    it is the line from the code I posted first.
     
  10. Offline

    pokuit

    @i3ick
    Not sure it will fix your problem probably not reading the other peoples statements but when your getting the world from the config do this
    Code:text
    1.  
    2. String sworld = this.getConfig().getString(redspawn.World);
    3. World word = getServer().getWorld(sworld);
    4.  

    I hope this helped
    pokuit
     
  11. Offline

    i3ick

    But I already have
    Object world = this.getConfig().get("redspawn" + ".World");

    isn't that the same thing?
     
  12. Offline

    Rocoty

    That won't get you an instance of type World, I'm sorry. I'd suggest you read world names, and use Bukkit.getWorld(the name retrieved);
     
  13. Offline

    i3ick

    I tried what pokuit said but it didn't work
     
  14. Offline

    ZeusAllMighty11

     
  15. Offline

    ZachBora

    i3ick Please share us what your new code looks like now and what the error you are getting shows now.
     
  16. Offline

    i3ick

    But it works until it's restarted, why?

    ZachBora I reverted everything to be the same as before because that worked a bit, everything else didn't work at all :/
     
  17. Offline

    ZeusAllMighty11

    World objects are not serializable.
    World objects are not serializable.
    World objects are not serializable.

    Not sure which one of those you can read. World objects are not persistent. Therefore, will not go through reloads
     
  18. Offline

    i3ick

    Ohhhh, I get it now. What should I do to get around this problem?
     
  19. Offline

    ZachBora

    i3ick Can't help without more of your code, there's like nothing up there. Idc what TheGreenGamerHD says about the world objects, we don't see your code so we can't be sure it's the problem. Unless he saw the whole thing?
     
  20. Offline

    ZeusAllMighty11

    Instead of storing worlds, just store the world name. To retrieve, use Bukkit.getWorld(nameAsString);
     
    Minnymin3 likes this.
  21. Offline

    i3ick

    Thanks, now the line looks like this and I am getting only the name
    But when I try to retrieve it I get an error here:
    and that line is connected to the line retrieving the world name (I think the problem is that I'm not retrieving it right):
     
  22. i3ick
    You need to use Bukkit.getWorld() to get the World object from the String name... people have told you this over and over.

    And next time you report some new error, post that error, because now you have (most likely) a class cast excception because you're converting String to World which just isn't possible without calling an actual method that translates that properly.... e.g. Bukkit.getWorld().
     
  23. Offline

    i3ick

    Ok, I think everything is setup fine now. I am first getting the string world (this works, I checked with printing the world name) then I am importing it into the world object and finally the object is inserted into the location.
    Code:
    String playerWorld = this.getConfig().getString("redspawn" + ".World");
       
    Object world = Bukkit.getServer().getWorld(playerWorld);
    Location redspawn = new Location((World) world, redspawnX, redspawnY, redspawnZ, redspawnYaw, redspawnPitch);
               
                    player.teleport(redspawn);
             
            }
    However I still get a NullPointerException:

    Line 87 is " player.teleport(redspawn); " (look at code above).
    I have went through the whole topic again and I think I did everything I was told, however if I missed something please point it out, thanks.

    i3ick
     
  24. i3ick
    Most likely the world you looked for doesn't exist or it's not loaded.

    You need to check if the resulting world from getWorld() is not null, and if it is print an error and stop the code there.

    Also, why are you keep saving as Object and casting ? Save directly as World.

    Here:
    Code:
    World world = Bukkit.getWorld(name);
    
    if(world != null)
    {
        // do your thing
    }
    else
    {
        getLogger().warning("The '" + name + "' world from config.yml does not exist or is not loaded !");
    }
    If you're not in the main class then use 'plugin.getLogger()' instead, plugin beeing the passed instance of your main class... it's good practice so if you followed a good tutorial they should've shown you that.
     
  25. Offline

    i3ick

    Digi
    Hey, this was a great tip! It turns out the world is indeed a null. I found this weird so I made a little script:

    Code:
           
    else if(cmd.getName().equalsIgnoreCase("showname")){
    String playerWorld = this.getConfig().getString("redspawn" + ".World");
    player.sendMessage(playerWorld);
    }
            }
    The output I get is CraftBukkit. So if this is the name of the world why does java say it's a null?


    I think I saw someone say it can be done like that. Changed it to World now.
     
  26. i3ick
    So... if it's not the default load and nobody is in it, then it's not loaded, therefore getWorld() can't get it.

    You need to use createWorld() to load (or create) a world.

    It's not required, you should just set the type of the variable to the return type of the method unless you need its superclass for some specific reason.
     
  27. Offline

    i3ick


    Hmmm... that's weird because when I executed the command to save the world and player location I was in the world myself.

    I'll try to set up the createWorld and will get back to you

    Digi

    Hey something very weird happened. The code works, but it took me to a place I've never been before.
    This is the complete code I use to get to the place where I saved my location.

    Code:
       
     
    else if(cmd.getName().equalsIgnoreCase("red")){
    int redspawnX = this.getConfig().getInt("redspawn" + ".X");
            int redspawnY = this.getConfig().getInt("redspawn" + ".Y");
            int redspawnZ = this.getConfig().getInt("redspawn" + ".Z");
            int redspawnYaw = this.getConfig().getInt("redspawn" + ".Yaw");
            int redspawnPitch = this.getConfig().getInt("redspawn" + ".Pitch");
            String playerWorld = this.getConfig().getString("redspawn" + ".World");
       
            World world = Bukkit.getWorld(playerWorld);
     
            if(world != null)
            {
          Location redspawn = new Location((World) world, redspawnX, redspawnY, redspawnZ, redspawnYaw, redspawnPitch);
          player.teleport(redspawn);
            }
            else
            {
          Bukkit.getServer().createWorld(new WorldCreator(playerWorld).environment(World.Environment.NORMAL));
                getLogger().warning("The '" + "redspawn" + ".World" + "' world from config.yml does not exist or is not loaded !");
            }
     
     
    }}

    EDIT:
    it seems like I've created an entirely different world. Why did this happen when I was already in one?

    Also when I die I get teleported back the the original world.

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

Share This Page