1

i am using collections.sort methods int compare to compare 2 objects distance parameter and sort them according to distance in ascending order. here is what i am doing to compare it

Collections.sort(venuesList, new Comparator<FoursquareVenue>() {

                @Override
                public int compare(FoursquareVenue lhs, FoursquareVenue rhs) {                  


                    return lhs.getDistance().compareTo(rhs.getDistance());  
                }
            });

I think there is just some small silly thing i am missing to do but i couldn't figure it out whats the problem error says something like this

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)

I think i need to handle the null values of the object but i don't know how can i do that?

1 Answer 1

1

The second argument to Log.v(...) needs to be a String and the getDistance() method probably returns null

To prevent the exception you can do something like this:

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Actually it is what you said.. it still throws same exception and how should i handle that if getDistance is returning null value...
@ChristianYoung Yes, I've changed my answer, it may be returning null hence the NullPointerException
you are right i just saw in Logcat that when lhs.getDistance returns null it shows null pointer exception so what should i do to handle that?
Log.v is just to print the output on my console to figure out whats wrong changing those value didn't effect the error i need to solve the return statement... consider Log.v statement is not in code
@ChristianYoung I've edited my question again, the Log.v(...) method was the one causing the error but I guess by fixing it you got another NullPointerException because lhs.getDistance() returned null when you were trying to compare it lhs.getDistance().compareTo(...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.