0

I have written an algorithm to calculate the distance of the track followed by a vehicle for one trip.Each point on the track has a (lat, lon). Here is my code,I am getting the error mentioned above.

List<TrackPoint> tp = new ArrayList<TrackPoint>();    

for (int i = 0; i < tp.size(); i++) {
    double lat1 = tp.get(i).getLat();                        
    double lat2 = tp.get(i + 1).getLat();
    double lon1 = tp.get(i).getLon();                       
    double lon2 = tp.get(i + 1).getLon();
    double radlat1 = Math.PI * lat1 / 180;
    double radlat2 = Math.PI * lat2 / 180;
    double theta = lon1 - lon2;
    double radtheta = Math.PI * theta / 180;
    double dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    dist = Math.acos(dist);
    dist = dist * 180 / Math.PI;
    dist = dist * 60 * 1.1515; 
    dist = dist * 1609.344;// distance in miles
    double _KM = 1000; //KM the unit of distance                       
    //distance in km
    double distInKM = dist / _KM;
    DecimalFormat df = new DecimalFormat("###.###");
    double dt = Double.parseDouble(df.format(distInKM));
    double mileage = 0;
    mileage += mileage + dt;       
    tp.get(i).setMileage(mileage);                       
}

Error:

java.lang.IndexOutOfBoundsException: Index: 283, Size: 283
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)

This error points to line: double lat2 = tp.get(i + 1).getLat(); I think the error is associated with the list size but i can't figure out why? Some one help me see what wrong i have.

Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
Reagan Ochora
  • 1,642
  • 1
  • 19
  • 22

5 Answers5

2

When you go to the last object (TrackPoint) in the loop, i + 1 element does not exist.

double lat2 = tp.get(i + 1).getLat();
Alex
  • 11,451
  • 6
  • 37
  • 52
1

The problem is here

tp.get(i + 1)

When i is equal to tp.size-1 you make it equal to tp.size(). To solve it change for loop to :

for (int i = 0; i < tp.size() - 1; i++) {
Jay Smith
  • 2,331
  • 3
  • 16
  • 27
0

When you go to the last object (TrackPoint) in the loop, (i + 1)th element does not exist. Hope the following code will be ok .

List<TrackPoint> tp = new ArrayList<TrackPoint>();    

for (int i = 0; i < tp.size()-1; i++) {
    double lat1 = tp.get(i).getLat();                        
    double lat2 = tp.get(i + 1).getLat();
    double lon1 = tp.get(i).getLon();                       
    double lon2 = tp.get(i + 1).getLon();
    double radlat1 = Math.PI * lat1 / 180;
    double radlat2 = Math.PI * lat2 / 180;
    double theta = lon1 - lon2;
    double radtheta = Math.PI * theta / 180;
    double dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    dist = Math.acos(dist);
    dist = dist * 180 / Math.PI;
    dist = dist * 60 * 1.1515; 
    dist = dist * 1609.344;// distance in miles
    double _KM = 1000; //KM the unit of distance                       
    //distance in km
    double distInKM = dist / _KM;
    DecimalFormat df = new DecimalFormat("###.###");
    double dt = Double.parseDouble(df.format(distInKM));
    double mileage = 0;
    mileage += mileage + dt;       
    tp.get(i).setMileage(mileage);                       
}
Christopher Marlowe
  • 2,098
  • 6
  • 38
  • 68
0

You loop iterates till list size. but in last iteration you are trying to get (i + 1) index that does not exists in your case.

double lat2 = tp.get(i + 1).getLat();

Correct this .get(i + 1).

0

All Above answers are correct. Alternatively can try with bellow code

List<TrackPoint> tp = new ArrayList<TrackPoint>();
    for (int i = 0; i < tp.size(); i++) {
    double lat1 = tp.get(i - 1).getLat();                        
    double lat2 = tp.get(i).getLat();
    double lon1 = tp.get(i - 1).getLon();                       
    double lon2 = tp.get(i).getLon();
    double radlat1 = Math.PI * lat1 / 180;
    double radlat2 = Math.PI * lat2 / 180;
    double theta = lon1 - lon2;
    double radtheta = Math.PI * theta / 180;
    double dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    dist = Math.acos(dist);
    dist = dist * 180 / Math.PI;
    dist = dist * 60 * 1.1515; 
    dist = dist * 1609.344;// distance in miles
    double _KM = 1000; //KM the unit of distance                       
    //distance in km
    double distInKM = dist / _KM;
    DecimalFormat df = new DecimalFormat("###.###");
    double dt = Double.parseDouble(df.format(distInKM));
    double mileage = 0;
    mileage += mileage + dt;       
    tp.get(i - 1).setMileage(mileage);
}
Zakaria
  • 19
  • 6