Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check LatLngBounds.Builder if null

This is mycode:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(int x = firstVisibleItem; x < lastVisibleItem; x++){
    builder.include(temp.getPosition());
}

However this line throws an error (java.lang.IllegalStateException: no included points :

double north = builder.build().northeast.latitude;

This is because the above loop didn't run at all so no points were included in the builder.

How can I check if the builder has at least one point?

I tried builder.build()!=null which throws the above error and builder!=null which is always True.

try{}catch(IllegalStateException e){ } works. Is it stupid to ask a !=null way? Mico-management? Thanks

like image 309
Diolor Avatar asked Dec 26 '22 13:12

Diolor


1 Answers

You can create a counter and use it to verify that you have at least one point.

int counter = 0;
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(int x = firstVisibleItem; x < lastVisibleItem; x++){
    counter++;
    builder.include(temp.getPosition());
}
if (counter > 0) {
    //use a LatLngBounds.Builder to build the LatLngBounds object
}
like image 168
Emil Adz Avatar answered Jan 05 '23 09:01

Emil Adz