Cannot measure distance between worlds error

Discussion in 'Plugin Development' started by civ77, Jan 2, 2012.

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

    civ77

    Bukkit is saying cannot measure distance between worlds or null:

    Code:
    2012-01-02 21:04:00 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'deposit_xp' in plugin ExpBank v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:165)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:378)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:757)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:722)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:715)
        at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:93)
        at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:527)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:425)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    Caused by: java.lang.IllegalArgumentException: Cannot measure distance between worlds or to null
        at org.bukkit.Location.distance(Location.java:364)
        at me.hayden.expbank.commands.CommandExecutor_Deposit_xp.onCommand(CommandExecutor_Deposit_xp.java:37)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 12 more
    and here's my command class:
    Code:
        if(client.getLocation().distance(plugin.BankLoc)<= 3){
        xp = client.getTotalExperience();
        client.setTotalExperience(0);
            storedXp = plugin.ExpBankAccs.get(client.getPlayerListName());
            plugin.ExpBankAccs.put(client.getPlayerListName(),storedXp + xp);
        }
    }
            }
    return false;}
    }
    and here's the main class:
    Code:
    //ExpBank - by Hayden
    //Created with Kickstarter
    
    
    package me.hayden.expbank;
    
    
    
    public class ExpBank extends JavaPlugin{
        private Logger log;
        private PluginDescriptionFile description;
    
        private String prefix;
        public double config_bankxcoord;
        public double config_bankycoord;
        public double config_bankzcoord;
        public String config_bankworld;
        public  Location BankLoc;
        public HashMap<String,Integer> ExpBankAccs = new HashMap<String,Integer>();
        @Override
        public void onEnable(){
            log = Logger.getLogger("Minecraft");
            description = getDescription();
            prefix = "["+description.getName()+"] ";
            File savefile = new File("plugins/ExpBank/save.dat");
            boolean exists = savefile.exists();
            if(exists){
                try {
                    ExpBankAccs = (HashMap<String,Integer>)SLAPI.load("plugins/ExpBank/save.dat");
                } catch (Exception e) {
                    log.info("!!!failed to load ExpBank accounts!!!");
                    e.printStackTrace();
                }
    
            }
            log("loading "+description.getFullName());
    
            setupConfiguration();
    
            Location BankLoc = new Location(Bukkit.getWorld(getConfig_bankworld()),getConfig_bankxcoord(),getConfig_bankycoord(),getConfig_bankzcoord()) ;
    
    
    
            getCommand("deposit_xp").setExecutor(new CommandExecutor_Deposit_xp(this));
            getCommand("withdraw_xp").setExecutor(new CommandExecutor_Withdraw_xp(this));
    
    
    
        }
    
        @Override
        public void onDisable(){
            log("disabled "+description.getFullName());
            try {
                SLAPI.save(ExpBankAccs,"plugins/ExpBank/save.dat");
            } catch (Exception e) {
                log.info("!!!failed to save ExpBank accounts!!!");
                e.printStackTrace();
            }
    
        }
        public void log(String message){
            log.info(prefix+message);
        }
    
    
        private void setupConfiguration(){
            Configuration cfg = getConfiguration();
    
            cfg.setHeader("#Select your experience bank's coordinates");
    
            config_bankxcoord = cfg.getDouble("BankXCoord", 0);
            config_bankycoord = cfg.getDouble("BankYCoord", 0);
            config_bankzcoord = cfg.getDouble("BankZCoord", 0);
            config_bankworld = cfg.getString("BankWorld", "world");
    
            cfg.save();
        }
    
    
    
        public Double getConfig_bankxcoord(){
            return config_bankxcoord;
        }
    
        public double getConfig_bankycoord(){
            return config_bankycoord;
        }
    
        public Double getConfig_bankzcoord(){
            return config_bankzcoord;
        }
    
        public String getConfig_bankworld(){
            return config_bankworld;
        }
    
    
    
    
    
    }
    
    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  2. You should format your post to get better help. Simply posting you error and code and telling people to find what's wrong isn't very helpful.

    First isolate the code which is causing errors and then post that, once you've done that I'd be more than willing to help.
     
  3. Offline

    DDoS

    You can't get the distance between two locations using the distance method provided by the Location class if the two locations come from different worlds.

    If location0 and location1 come from different worlds, doing location0.distance(location1) won't work.

    Also learn how to read a stack trace.
     
  4. Offline

    civ77

    @Adamki11s I reformatted and I'd really appreciate help.
    edit: @DDoS they aren't coming from different worlds... I understand how to read a stack trace...
     
  5. Offline

    DDoS

    Well, Craftbukkit disagrees.

    Try adding a check, making sure the worlds from both locations are equal.

    Edit: also make sure that neither locations are null.
     
  6. Offline

    civ77

    How can the location be null?
     
  7. @civ77 Assuming you know how to read a stack trace you should have identified the code in question as i mentioned earlier which is clearly being caused by this line:

    at me.hayden.expbank.commands.CommandExecutor_Deposit_xp.onCommand(CommandExecutor_Deposit_xp.java:37)

    Post the code for that line and maybe we can help. You have just posted your classes and I have no way of knowing what code is in what class and you cannot expect me to count the lines.
     
  8. Offline

    civ77

    @Adamki11s I specifically posted more of the code because it was necessary to understand the problem.
    Code:
    Player client = (Player) sender;
    if(client.getLocation().distance(plugin.BankLoc)<= 3)
     
  9. Ok well for this error it is easier to find an alternative way to get the distance.

    Code:java
    1.  
    2. double x1, x2, y1, y2, z1, z2;
    3. x1 = client.getLocation.getX();
    4. y1 = client.getLocation.getY();
    5. z1 = client.getLocation.getZ();
    6. x2 =plugin.BankLoc.getX();
    7. y2 =plugin.BankLoc.getY();
    8. z2 =plugin.BankLoc.getZ();
    9.  
    10. //therefore using pythagorus theorem
    11. double distance = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) + Math.pow(z1 - z2, 2));

    Easy enough ;)
     
    civ77 likes this.
  10. Offline

    civ77

    ty, hope that fixes it.

    Edit: @Adamk11s "getLocation cannot be resolved or is not a field" what does that mean?
    Edit 2: nvm you forgot to put () after getLocation, TY

    I tried that but it's still giving the same error :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  11. Hmm, you really shouldn't as we are using our own distance method.

    Are you sure that neither location is null?

    Code:java
    1.  
    2. if(plugin.BankLoc == null || client.getLocation() == null){
    3. System.out.println("A location was null!");
    4. return;
    5. }
    6.  
     
  12. Offline

    civ77

    @adamk11s I'm going to sleep now but here's the current error (I tried what you last suggested)
    Code:
    2012-01-02 15:30:00 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'deposit_xp' in plugin ExpBank v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:165)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:370)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:756)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:721)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:714)
        at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
        at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:516)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:414)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    Caused by: java.lang.IllegalArgumentException: Cannot measure distance between worlds or to null
        at org.bukkit.Location.distance(Location.java:355)
        at me.hayden.expbank.commands.CommandExecutor_Deposit_xp.onCommand(CommandExecutor_Deposit_xp.java:37)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 12 more
    
     
  13. It says that you have a null:
    And where:
    So is it hard to mentally imagine what could be null on that line or the ones that relate to it ? :p
     
  14. You must be still using that method somewhere as our own method does not throw that error, also as Digi reinforced you need to check for nulls as I stated in my previous post ^^.
     
  15. Offline

    civ77

    I did check for nulls:
    Code:
            if (command.getName().equalsIgnoreCase("deposit_xp")) {
                int xp;
                int storedXp;
        Player client = (Player) sender;
        double x1, x2, y1, y2, z1, z2;
        x2 =plugin.BankLoc.getX();
        y2 =plugin.BankLoc.getY();
        z2 =plugin.BankLoc.getZ();
        x1 = client.getLocation().getX();
        y1 = client.getLocation().getY();
        z1 = client.getLocation().getZ();
        double distance = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) + Math.pow(z1 - z2, 2));
                if (!(sender instanceof Player)) {
                    sender.sendMessage("This command is only applicable to players.");
                    return true;
                }
    if(sender instanceof Player){
        if(plugin.BankLoc == null || client.getLocation() == null){
            plugin.log.info("A location was null!");
            return true;
        }
    
        if(client.getWorld()==Bukkit.getWorld(plugin.getConfig_bankworld())){
        if(distance<= 3){
        xp = client.getTotalExperience();
        client.setTotalExperience(0);
            storedXp = plugin.ExpBankAccs.get(client.getPlayerListName());
            plugin.ExpBankAccs.put(client.getPlayerListName(),storedXp + xp);
        }
        else{
            sender.sendMessage("Please go to the bank.");
        }
        }
        else{
            sender.sendMessage("Please go to the bank.");
        }
    }
        }
    return false;}
    }
    It cannot be null because a players location cannot be null nor can a the location in a world because it is set in the configuration (which defaults to 0.0,64.0,0.0)
     
  16. Of course a location object can be null. If you are using a weak reference or think you are loading data into it which is actually null data.
     
  17. Offline

    civ77

    @Adamki11s "weak reference"?? as I said I'm sure I'm not loading null data into it but what's a weak reference?
    Edit: I still don't still fully understand what a weak reference is but as far as I can tell could the BankLoc variable not being locally used cause garbage collection to fill it with nulls?
     
  18. You shouldn't be getting weak reference issues in that space of time.

    What you should do is just print out both locations and try to recompile and re-run and make sure you are not using Bukkits method to compare distances anywhere as that seems to be what is throwing the error.

    Simply put a weak reference is when you create a pointer to an address in memory such as a cache. You can reference the data however if the originating source of the data changes then the weak reference will still point to the same place even though the data is no longer there which will result in null. This is only a problem when the Java garbage collector is invoked and generally will only over happen if you are not referencing to that object much, anyway nevermind about that just try my sugegstion because you shouldn't be getting the IllegalArgumentException if you are using my method.
     
  19. Offline

    civ77

    @Adamki11s I'm using deprecated configuration methods, could this be causing the world part of the Location BankLoc to be null?
     
  20. It is deprecated but I think it still works for now. Just try printing our both locations to see if there are null or not as I suggested earlier.
     
  21. Offline

    civ77

    I have done that and it wont print the numbers and it still giving the null error. Does its lack of printing anything mean they are null?
     
  22. What do you mean it wont print? If you cant see anything then it is probably loading spaces or something otherwise you would get a null.
     
  23. Offline

    civ77

    I mean that even though I have it doing println(x1) it prints nothing in the console.
     
  24. Offline

    Father Of Time

    you need to eliminate variables. if I were you I would comment out EVERYTHING in that entire function except for:
    log.info( location1.toString() );
    log.info( location2.toString() );

    If you comment everything else out with /* CODE */ you can eliminate all other code and purely focus on the state of your to locations.

    by printing both locations you can:
    1) verify neither is null
    2) verify the world of both locations.

    If you remove all other code temporally it will stop that error from occurring and allow you to retrieve the data you need. If the problem persist past commenting everything out then it's clear your issue resides elsewhere.

    When a not so obvious problem presents itself you must eliminate as many variables as possible to narrow the scope of your search.

    I hope this helps.
     
  25. Offline

    civ77

    Code:
        Player client = (Player) sender;
        z1 = client.getLocation().getZ();
    The problem seems to be in that second line. UNLESS the compiler does not count blank lines or commented lines.
     
  26. Offline

    Father Of Time

    We understand that, but just because that is the line that is "triggering" the error doesn't mean that the problem resides on that line. For instance... If you are getting a null point exception on line 20 that doesn't mean that your problem is on line 20, that means that the code that is crashing the compiler is on line 20. The actual problem resides somewhere else where the variable that is null on line 20 is assigned.

    If you do as I suggested above it would help clarify a lot, try it and post the console print. Sadly you need to do a little work and do some debugging, otherwise we won't be able to assist you any further.
     
  27. Offline

    civ77

    I understand that, I was being pretty unclear with my previous comment though. I meant that the null seems to occurring when it tries to get the players Z coordinate.

    EDIT: this is where the variable is assigned (if I understand what assigning a variable means)
    Code:
    double x1, x2, y1, y2, z1, z2;
     
  28. Offline

    Father Of Time

    Not quiet, that is where your variables are being declared, not assigned. This is declaring:

    Code:
    String example;
    and this is assigning:

    Code:
    example = "variable data";
    Declaring is when you create the variable that holds your data, assigning is when you take that variable and store data within it.

    If you didn't declare the variable you would get a different error, an object reference error; but your error is a null exception which means that the declaration occurred (it can find the variable) but the data within the variable is null and missing.
     
  29. Offline

    civ77

    I don't understand what could cause it to be missing? do I need to make the variables public?
     
  30. Offline

    Father Of Time

    No that's not it, if that was the case you would get an inaccessibility error, not a null exception.

    I've got to be completely honest, you are making assisting you extremely difficult because you don't explain your efforts. You don't share with us what you've changed since your last effort, you haven't shown us where the variables are being assigned, you really haven't done anything that we've instructed you to do.

    Your issue is with your variables being incorrectly assigned to. Your project appears to break down a Location to its basic variables ( World, X, Y, Z ) and then later tries to take those 4 pieces of data and convert them back into a location.

    Simply put, those variables you showed us 3 post ago ( the x, y, z, etc) are not being assigned properly... One of those 4 variables is null, so where ever you are "retrieving" this data and attempting to creating the new location you get a crash. You need to print each one before you do the assigning.

    Code:
    log.info( "Debug: " + Source.getX() );
    log.info( "Debug: " + Source.getY() );
    log.info( "Debug: " + Source.getZ() );
    log.info( "Debug: " + Source.getWorld() );
    
    Location locx = Source.getX();
    Location locy = Source.getY();
    Location locz = Source.getZ();
    World world = Source.getWorld();
    
    Location newloc = new Location( world, locx, locy, locz );
    
    If you try to print the information AFTER the location is created you will get no print statement, because the server has already crashed; instead you need to grab the information from its source, print it, then try to create the location. Since the data has been retrieved and printed before it was converted into a location you should get a console print before the server crashes.

    You need to work backwards mate, track the data being passed around as far back as possible until the console print matches your expected results; this is something we simply can't do for you...

    I wish you luck with your project, but at this time without a little more effort on your behalf I'm simply going to have to terminate my assistance, because we are simply talking in circles. :oops:
     
Thread Status:
Not open for further replies.

Share This Page