[SOLVED] Well ... I'm stuck with a stupid nullpointer ...

Discussion in 'Plugin Development' started by number1_Master, Aug 24, 2012.

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

    number1_Master

    Final EDIT: I figured out the issue. It was simply my bad searching in the config.yml. I simply needed to check if the location in the config started with the location I was searching, instead of seeing if the location int he config contained the location I was searching.






    EDIT: I have confirmed that the Null Pointer is caused because of a messed up search engine

    I'm creating a plugin for my server where when you go through portals saved in the config, they get teleported to a different world. Example: I go through a portal at spawn which is saved in the config. The config states that the portal leads to x world.

    List of my worlds: world, world_nether, world_the_end, GameWorld, Creative
    Saving the world to the config is just fine and checking if the portal is in the config works fine too (it is saved as a string). The issue I'm having is the teleporting. The issue is caused by a bad search. I cannot figure this f**k!*g out.

    Code in listener:
    Show Spoiler
    Code:java
    1.  
    2. private DGWorldManager plugin;
    3. public DGWorldManagerListener(DGWorldManager instance)
    4. {
    5. plugin = instance;
    6. }
    7.  
    8. @EventHandler
    9. public void onPlayerPortal(PlayerPortalEvent e)
    10. {
    11. if(e.isCancelled()) return;
    12.  
    13. final Player player = e.getPlayer();
    14. final ArrayList<String> links = (ArrayList<String>) plugin.getConfig().getStringList("Links");
    15. int x = (int) Math.round(player.getLocation().getX());
    16. int y = (int) Math.round(player.getLocation().getY());
    17. int z = (int) Math.round(player.getLocation().getZ());
    18. String loc;
    19.  
    20. for(int a = -2; a <= 2; a++)
    21. {
    22. int xa = x + a;
    23. if(Utils.get(links, "" + xa, 'X') != null)
    24. {
    25. loc = xa + " ";
    26. for(int b = -3; b <=3; b++)
    27. {
    28. int yb = y + b;
    29. if(Utils.get(links, "" + yb, 'Y') != null)
    30. {
    31. loc = loc + yb + " ";
    32. for(int c = -2; c <= 2; c++)
    33. {
    34. int zc = z + c;
    35. if(Utils.get(links, "" + zc, 'Z') != null)
    36. {
    37. loc = loc + zc;
    38. //Error Here \/
    39. String tpWorld = (String) Utils.get(links, loc, 'W');
    40. final Location tpLoc = Bukkit.getServer().getWorld(tpWorld).getSpawnLocation();
    41. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
    42. {
    43. public void run()
    44. { player.teleport(tpLoc); }
    45. }, 20);
    46. return;
    47. }
    48. }
    49. }
    50. }
    51. }
    52. }

    Code in Utils:
    Show Spoiler
    Code:java
    1. public static Object get(ArrayList<String> links, String loc, char type)
    2. {
    3. if(checkLinks(links, loc))
    4. {
    5. String[] split = getLink(links, loc).split(" ");
    6. try
    7. {
    8. switch(type)
    9. {
    10. case 'X': return Integer.parseInt(split[0]);
    11. case 'Y': return Integer.parseInt(split[1]);
    12. case 'Z': return Integer.parseInt(split[2]);
    13. case 'W': return split[3];
    14. }
    15. }
    16. { }
    17. }
    18. return null;
    19. }
    20. public static boolean checkLinks(ArrayList<String> links, String loc)
    21. {
    22. if(links.isEmpty())
    23. {
    24. returnfalse;
    25. }
    26. for(String portal : links)
    27. {
    28. if(portal.contains(loc)) return true;
    29. }
    30. return false;
    31. }
    32.  
    33. public static String getLink(ArrayList<String> links, String loc)
    34. {
    35. for(String portal : links)
    36. {
    37. if(portal.contains(loc)) return portal;
    38. }
    39. return" ";
    40. }


    NOTES: This code does not work only when I'm getting the world. When it comes to getting the coordinates, it works just fine.



    Things I have tried / changed:
    - I got rid of the if statement since I already check if the world is null when creating the location.
    * This error continues on a server with no plugins
    * Using plugin instead of Bukkit doesn't change anything
    * The error is not occurring because it is empty
    * It is returning the right location
    * The error is caused because of too many locations and a bad search. Because of the bad search, it is trying to get a null world.
     
  2. Offline

    sternmin8or

    You cant just call getWorld(String), String is a class. Rename your variable to something else.
     
  3. Offline

    ZeusAllMighty11

    ^ This.



    I suggest also using another variable for each world...
     
  4. Offline

    number1_Master

    I tried that way before. Removed the error, but not fixed.

    I'll do it again if it will make you happy.
     
  5. Offline

    keensta

    Doesn't
    Code:
    World tpWorld = Bukkit.getServer().getWorld((String) Utils.get(links, loc, 'W'));
    give you errors it self didn't think you could add Utils.get(links, loc, 'W') to the .getWorld(String) part.
     
  6. Offline

    Firefly

    If you notice he's casting the return of his Utils method to String, not just passing in String.
     
  7. Offline

    number1_Master

    The IF statement is where the error always occurs.I changed my code a little and same error.
    Code:java
    1. String tpWorld = (String) Utils.get(links, loc, 'W');
    2. if(Bukkit.getServer().getWorld(tpWorld) != null)
    3. {
    4. final Location tpLoc = Bukkit.getServer().getWorld(tpWorld).getSpawnLocation();
     
  8. Offline

    Firefly

    Are you sure it's that if statement and your stack trace isn't out of sync with your code?
     
  9. Offline

    number1_Master

    2012-08-24 14:52:08 [SEVERE] Could not pass event PlayerPortalEvent to DGWorldManager
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:332)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at net.minecraft.server.ServerConfigurationManagerAbstract.changeDimension(ServerConfigurationManagerAbstract.java:473)
    at net.minecraft.server.EntityPlayer.g(EntityPlayer.java:239)
    at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:128)
    at net.minecraft.server.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:581)
    at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
    at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
    at org.bukkit.craftbukkit.CraftServer.getWorld(CraftServer.java:786)
    at me.number1_Master.DGWorldManager.DGWorldManagerListener.onPlayerPortal(DGWorldManagerListener.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:330)
    ... 13 more
     
  10. Offline

    MucTweezer

    I could be wrong here, but it seems to me that if the line "if(Bukkit.getServer().getWorld(tpWorld) != null)" is throwing a NPE, then it must be because Bukkit.getServer() is returning null.

    I'm pretty new here, so I might be completely out to lunch.

    However, do you have an instance of your main plugin in your listener? Perhaps calling plugin.getServer() might work. Just a random thought.
     
  11. Offline

    number1_Master

    I do have an instance. I have tried running plugin.getServer(). I will test it again just to double check.

    EDIT: Still error.
     
  12. Offline

    sternmin8or

    It didnt have those extra parenthesis when I commented.

    try declaring everything befroe the if statement like this:

    String tpWorld = (String) Utils.get(links,loc,'W');
    World world = getServer().getWorld(tpWorld);
    if (world !=null){
    }
     
  13. Offline

    number1_Master

    1.) You don't need to show me how to do it. I'm smart :p
    2.) As soon as I saw that code, I was wondering if World world = getServer().getWorld(tpWorld); would through a null pointer. I tested it, and it did.

    The error is caused from getting the world .... mhm
     
  14. Offline

    sternmin8or

    lol sorry, anyways this is a long shot, but have you tried running your plugin without anything else on the server
     
  15. Offline

    number1_Master

    Doesn't fix it.

    Updated the original post with things I have done.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  16. Offline

    Giant

    Your issue, is actually NOT on that line, it is more or less more likely that you are passing getWorld a null. What I mean by this is that I have a suspicion that String tpWorld is actually null, which would be caused by Utils.get, not returning the string you had expected!

    To test this out you could output tpWorld somewhere before you try to use it. Say, plugin.getLogger().warning("Value of tpWorld: " + tpWorld);
     
  17. Offline

    number1_Master

    I've tested it out before. If I go from world to Gameworld, it prints out GameWorld because ... well it can load the gameworld. But if I try to go from GameWorld to world, it prints out the nullpointer exception. I don't understand why 'world' turns out to be null...
     
  18. Offline

    Giant

    It would seem that the reason would then be in Utils, so we would have to look there, to actually find out I suppose...
    And to me it seems Utils.checkLinks is returning false on "world". So either your ArrayList<String> links gets malformed, or checkLinks handles it badly...
     
  19. Offline

    number1_Master

    You can check the code above. I don't know why it works for one world, but not the others ...
     
  20. Offline

    Giant

    That only holds the Utils.get() method, as I said before though, I suspect it's either your ArrayList<String> not being what you expect, or your checkLinks handling it wrong. Would it be possible to include a little more code?
     
  21. Offline

    number1_Master

    One more thing I want to say before I include the code.

    All code works correctly if I go from the world to x world. The code does not work if I try to from x world to y world.

    Posted entire utils class.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  22. Offline

    Giant

    That code works in one way, but does not in the other, kind of already indicates that there are bugs in it, doesn't it? And as I've stated already, it seems to be in Utils.checkList(), or one of the other arguments you pass to Utils.get(). But considering the minimal amount of shown code, it can't be said which or what is broken. Make sense?
     
  23. Offline

    number1_Master

    I posted the Utils class. Click the spoiler if you haven't checked. That is all the code that is used to get the world.
    I've said that 3 times.
     
  24. Offline

    Giant

    I've noticed you posted the code, I hadn't when I was still typing that though, as it was more or less aimed at the comment before your for last post.

    Now it would seem to me, checkLinks returns false if either the list is empty, or if no portal contains loc. So the first obvious step, is making sure that the list is not empty when you go back to "world", to do this add some debug stuff to the isEmpty() check.
     
  25. Offline

    number1_Master

    I did debug check before I actually posted this thread. I made it post the location whenever it found x y z. It did that correctly. Then I made it so it gets the world, displays the world, then teleports the player to the world. If I were in the world 'world', this would work. If I were in any other world, it would not be able to get the world via Bukkit.getServer().getWorld().
     
  26. Offline

    Giant

    But the NPE is on the getWorld() line, correct? That indicates that it's not related to getWorld() returning null yet. But either to getServer() or Bukkit being null, which is quite unlikely. And that leaves one option left, which is the parameter you supply to it. And yes I know it works when you go from world x to y, but that doesn't mean your code does not contain a bug. Does it work when you go from world y to say world z? If not then to me it seems most likely that either the List does not contain the location when you go back, perhaps you aren't setting it back correctly?
     
  27. Offline

    number1_Master

    I did a lot of debugging.
    It is not returning empty.
    It is returning the correct location from the config.
    It cannot get the world.

    I have one more thing to try.

    Time to bring someone who probably knows what the hell is wrong.
    ferrybig ? What do you think?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  28. You need to read the entire stack until up top... it doesn't end with your code:
    That's where the problem is triggered... the code at that line from CB source:
    Code:
        public World getWorld(String name) {
            return worlds.get(name.toLowerCase()); // << line 786
        }
    And guess what, the only 2 possible reasons for that line to trigger a NPE are:
    - worlds internal variable is null - *very* unlikely, if that were so, the entire server would just crash
    - name is null - which is sent by your plugin :) most likely the cause

    So, check your code, where you give the getWorld() the name, that name is returning null for some reason... trace the code back and see where it might not be asigned or it might be asigned to null.
     
  29. Offline

    MucTweezer

    This might be a long shot, but Digi's research above suggests to me that maybe world names must be in all lower case.
     
  30. Offline

    Giant

    Which pretty much is what I have been saying for a while...
     
Thread Status:
Not open for further replies.

Share This Page