vendredi 26 juin 2015

before() failing while comparing with nulls

I am using .before() function to make sure the older value is less than the newer value but the issue is that I am allowing the user to have nulls in any of those two values so when .before() compares the value to null... it fails.

My code:

public getDetails(Calendar older, Calendar newer) {
    if (older.before(newer)) {
        System.out.println("Works");
    } else {
        System.out.println("Works");
    }
}

One alternative I thought of was to check if ANY of the value is null and have the function pass.

So I tried:

if ((older || newer) == null || older.before(newer))

Is there a cleaner and shorter way to doing this?

Aucun commentaire:

Enregistrer un commentaire