Error: Array Out Of Bounds Error Details: Failed invoking <symbol> Help! No idea whats wrong.

The error is at line line 11, I don't really understand what's wrong with the array or how it's out of bounds, this code was shared with me so I'm not 100% sure how it works as I'm very much a coding noob.

I'm trying to draw a route.

var track = Application.Storage.getValue("track");
        var lon = Application.Storage.getValue("tracklon");
        var lat = Application.Storage.getValue("tracklat");

 if(track != null && lon != null && lat != null){
        var lonMax = 180.0;
        var lonMin = -180.0;
        var latMax = 90.0;
        var latMin = -90.0;
            for(var i=0; i < track.size(); i++) {
            lonMax = (lonMax > lon[i]) ? lonMax : lon[i];
            lonMin = (lonMin < lon[i]) ? lonMax : lon[i];
            latMax = (latMax > lat[i]) ? latMax : lat[i];
            latMin = (latMin < lat[i]) ? latMax : lat[i];
            }
            var scaleX = dc.getWidth() / (lonMax - lonMin);
            var scaleY = dc.getHeight() / (latMax - latMin);
            var scaleXY = (scaleX < scaleY) ? scaleX : scaleY;
            for(var i=0; i<track.size();i++){
                var pixelsLon = (lon[i+1] - lon[i]);
                var pixelsLat = (lat[i+1] - lat[i]);
                
                var displayX = pixelsLon + 120;
                var displayY = pixelsLat + 120;
               
                dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_TRANSPARENT);
                dc.fillCircle(displayX, displayY, 3);
                dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_TRANSPARENT);
                dc.fillCircle(displayX, displayY, 2);
        }}

  • It should read

    for(var i=0; i < lon.size(); i++)

    or

    for(var i=0; i < lat.size(); i++)

    As both array should have the same size.

  • Why don't you scale with scaleXY?

    it should read

    var pixelsLon = (lon[i+1] - lon[i]) * scaleXY;
    var pixelsLat = (lat[i+1] - lat[i]) * scaleXY;

  • var lonMax = -180.0;
    var lonMin =  180.0;
    var latMax =  -90.0;
    var latMin =   90.0;
    for(i=0; i < lon.size(), i++) {
        lonMax = (lonMax > lon[i]) ? lonMax : lon[i];
        lonMin = (lonMin < lon[i]) ? lonMax : lon[i];
        latMax = (latMax > lat[i]) ? latMax : lat[i];
        latMin = (latMin < lat[i]) ? latMax : lat[i];
    }
    

    Why do you change the signs of the start values (lonMax, lonMin, latMax, latMin)?

    For searching an array for a maximum value (e.g. lonMax) the variable should be initialized with a minimum value (e.g. lonMax = -180.0) and vice versa.

  • Typing mistake, I make a lot of those.

  • Also been doing this in spare moments which are fewer at the mo so am rushing when I type.

  • copy/paste your code to avoid typos that waste the time of people trying to help and start using println calls to understand what is happening in your code.

  • I did copy and paste the code, the typing mistakes were done in eclipse. Some just weren’t enough to cause an error hence why they went unnoticed. I will try and remember to add printIn calls however.

  • hey sorry to annoy you with more questioning but i've now run into a different problem.

    if(track != null){
    var lonMax = -180.0;
    var lonMin = 180.0;
    var latMax = -90.0;
    var latMin = 90.0;
    for(var i=0; i < lat.size(); i++) {
        lonMax = (lonMax > lon[i]) ? lonMax : lon[i];
        lonMin = (lonMin < lon[i]) ? lonMax : lon[i];
        latMax = (latMax > lat[i]) ? latMax : lat[i];
        latMin = (latMin < lat[i]) ? latMax : lat[i];
        
        System.println("lonmax = " + lonMax);
        System.println("latmax = " + latMax);
        System.println("lonmin = " + lonMin);
        System.println("latmin = " + latMin);
    
    }
    
    var scaleX = dc.getWidth() / (lonMax - lonMin);
    var scaleY = dc.getHeight() / (latMax - latMin);
    var scaleXY = (scaleX < scaleY) ? scaleX : scaleY;
               
        System.println(scaleX);
        System.println(scaleY);
        System.println("scxy " + scaleXY);
                
    for(var i=0; i < (lat.size() - 2); i+=2){
    
    var pixelsLon = (lon[i+1] - lon[i]) * scaleXY;
    var pixelsLat = (lat[i+1] - lat[i]) * scaleXY;
                    
    var displayX = pixelsLon + 120;
    var displayY = pixelsLat + 120;
        System.println("draw locx = " + displayX);
        System.println("draw locy = " + displayY);
                   
        dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_TRANSPARENT);
        dc.fillCircle(displayX, displayY, 3);
        dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_TRANSPARENT);
        dc.fillCircle(displayX, displayY, 2);
        }
    }

    This is the code i've got as a whole from the conversation, I am unsure if it is 100% correct as it's returning an invalid value on line 20 though has occured on line 19. I added println calls as Jim suggested and got this for the lon min/max/latmin/latmax calculations. I used a calculator and did the calculation in the code and some appear to return 0 which I think it dislikes, is that intentional? 

    lonmax = 0.206338
    latmax = 50.852655
    lonmin = 0.206338
    latmin = 50.852655


    lonmax = 0.206338
    latmax = 50.861510
    lonmin = 0.201533
    latmin = 50.861510


    lonmax = 0.206338
    latmax = 50.861510
    lonmin = 0.067743
    latmin = 50.830709


    lonmax = 0.206338
    latmax = 50.861510
    lonmin = 0.206338
    latmin = 50.861510


    lonmax = 0.206338
    latmax = 50.861510
    lonmin = 0.194795
    latmin = 50.856027


    lonmax = 0.206338
    latmax = 50.919657
    lonmin = 0.109084
    latmin = 50.919657


    lonmax = 0.218890
    latmax = 50.945271
    lonmin = 0.218890
    latmin = 50.945271


    lonmax = 0.281672
    latmax = 51.753031
    lonmin = 0.281672
    latmin = 51.753031


    lonmax = 0.281672
    latmax = 51.753777
    lonmin = 0.163367
    latmin = 51.753777

    Also I am again unsure if the code I've used is the correct one for scaling the route to follow in storage to my map of europe:

    It extends off screen (see below image) and changes as you move like a satnav (i've got a bit mixed up from all the examples you've kindly provided due to all my question asking). 

    The map displayed on the watch, it's a bit faint as I need to modify it.

    I hope this adequately explains everything.

  • Regarding my last issue, I figured out the problem, where it does the calculations:

    lonMax = (lonMax > lon[i]) ? lonMax : lon[i];

    lonMin = (lonMin < lon[i]) ? lonMax : lon[i];

    latMax = (latMax > lon[i]) ? lonMax : lon[i];

    lonMin = (lonMin < lon[i]) ? lonMax : lon[i];

    (as well as the lat equivalent calculations)

    I changed it to

    lonMax = (lonMax > lon[i]) ? lonMax : lon[i];

    lonMin = (lonMin < lon[i]) ? lonMin : lon[i];

    latMax = (latMax > lon[i]) ? lonMax : lon[i];

    lonMin = (lonMin < lon[i]) ? lonMin: lon[i];

    I’m very grateful for all of your help with this.