我正在使用collections.sort方法int compare来比较两个对象的距离参数,并根据距离以升序对它们进行排序。下面是我正在做的比较
Collections.sort(venuesList, new Comparator<FoursquareVenue>() {
@Override
public int compare(FoursquareVenue lhs, FoursquareVenue rhs) {
return lhs.getDistance().compareTo(rhs.getDistance());
}
});我想我只是遗漏了一些小的愚蠢的事情,但是我不能弄清楚问题是什么,错误是这样的
07-12 14:52:51.018: E/AndroidRuntime(1377): FATAL EXCEPTION: main
07-12 14:52:51.018: E/AndroidRuntime(1377): java.lang.NullPointerException:println needs a message
07-12 14:52:51.018: E/AndroidRuntime(1377): at android.util.Log.println_native(Native Method)
07-12 14:52:51.018: E/AndroidRuntime(1377): atandroid.util.Log.v(Log.java:117)我认为我需要处理对象的空值,但我不知道如何才能做到这一点?
发布于 2015-07-12 13:40:42
Log.v(...)的第二个参数需要是String,并且getDistance()方法可能返回null
为了防止异常,你可以这样做:
if (lhs != null && rhs != null && lhs.getDistance() != null && rhs.getDistance() != null) {
Log.v("lhs Distance", lhs.getDistance());
Log.v("rhs Distance", rhs.getDistance());
return lhs.getDistance().compareTo(rhs.getDistance());
} else {
return -1;
}https://stackoverflow.com/questions/31364715
复制相似问题