Skip to content

Commit 0ec77b3

Browse files
committed
Add check for size of places array
1 parent bf51c97 commit 0ec77b3

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java

+38-32
Original file line numberDiff line numberDiff line change
@@ -54,41 +54,47 @@ public NearbyPlacesInfo loadAttractionsFromLocation(LatLng curLatLng) {
5454
}
5555
List<Place> places = nearbyPlaces.getFromWikidataQuery(curLatLng, Locale.getDefault().getLanguage());
5656

57-
LatLng[] boundaryCoordinates = {places.get(0).location, // south
58-
places.get(0).location, // north
59-
places.get(0).location, // west
60-
places.get(0).location};// east, init with a random location
61-
62-
if (curLatLng != null) {
63-
Timber.d("Sorting places by distance...");
64-
final Map<Place, Double> distances = new HashMap<>();
65-
for (Place place: places) {
66-
distances.put(place, computeDistanceBetween(place.location, curLatLng));
67-
// Find boundaries with basic find max approach
68-
if (place.location.getLatitude() < boundaryCoordinates[0].getLatitude()) {
69-
boundaryCoordinates[0] = place.location;
70-
}
71-
if (place.location.getLatitude() > boundaryCoordinates[1].getLatitude()) {
72-
boundaryCoordinates[1] = place.location;
73-
}
74-
if (place.location.getLongitude() < boundaryCoordinates[2].getLongitude()) {
75-
boundaryCoordinates[2] = place.location;
76-
}
77-
if (place.location.getLongitude() > boundaryCoordinates[3].getLongitude()) {
78-
boundaryCoordinates[3] = place.location;
57+
if (places.size() > 0) {
58+
LatLng[] boundaryCoordinates = {places.get(0).location, // south
59+
places.get(0).location, // north
60+
places.get(0).location, // west
61+
places.get(0).location};// east, init with a random location
62+
63+
64+
if (curLatLng != null) {
65+
Timber.d("Sorting places by distance...");
66+
final Map<Place, Double> distances = new HashMap<>();
67+
for (Place place : places) {
68+
distances.put(place, computeDistanceBetween(place.location, curLatLng));
69+
// Find boundaries with basic find max approach
70+
if (place.location.getLatitude() < boundaryCoordinates[0].getLatitude()) {
71+
boundaryCoordinates[0] = place.location;
72+
}
73+
if (place.location.getLatitude() > boundaryCoordinates[1].getLatitude()) {
74+
boundaryCoordinates[1] = place.location;
75+
}
76+
if (place.location.getLongitude() < boundaryCoordinates[2].getLongitude()) {
77+
boundaryCoordinates[2] = place.location;
78+
}
79+
if (place.location.getLongitude() > boundaryCoordinates[3].getLongitude()) {
80+
boundaryCoordinates[3] = place.location;
81+
}
7982
}
83+
Collections.sort(places,
84+
(lhs, rhs) -> {
85+
double lhsDistance = distances.get(lhs);
86+
double rhsDistance = distances.get(rhs);
87+
return (int) (lhsDistance - rhsDistance);
88+
}
89+
);
8090
}
81-
Collections.sort(places,
82-
(lhs, rhs) -> {
83-
double lhsDistance = distances.get(lhs);
84-
double rhsDistance = distances.get(rhs);
85-
return (int) (lhsDistance - rhsDistance);
86-
}
87-
);
91+
nearbyPlacesInfo.placeList = places;
92+
nearbyPlacesInfo.boundaryCoordinates = boundaryCoordinates;
93+
return nearbyPlacesInfo;
94+
}
95+
else {
96+
return null;
8897
}
89-
nearbyPlacesInfo.placeList = places;
90-
nearbyPlacesInfo.boundaryCoordinates = boundaryCoordinates;
91-
return nearbyPlacesInfo;
9298
}
9399

94100
/**

0 commit comments

Comments
 (0)