I wanted to update the lastModifiedDate to current date and perform some action, only if the current date is greater than lastModifiedDate.
But I was concerned about getting NullPointerException near "dateToday.isAfter(lastModifiedDate.get("lastUpdated"))" inside if condition, when executed at the first time because, there is no key-value pair for "lastUpdated" key, yet in the HashMap. Below is the code.
import java.util.HashMap;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
public class TestDate {
static HashMap<String, LocalDate> lastModifiedDate = new HashMap<>();
static int unityResponsesCount;
public void resetUnityResponsesCount() {
unityResponsesCount = 0;
}
/**
* @param args
*/
public static void main(String[] args) {
LocalDate dateToday = LocalDate.now(DateTimeZone.getDefault());
if (lastModifiedDate.isEmpty() || dateToday.isAfter(lastModifiedDate.get("lastUpdated"))) {
lastModifiedDate.put("lastUpdated", dateToday);
TestDate testDate = new TestDate();
testDate.resetUnityResponsesCount();
}
}
}
Before running, I debugged the code and when I inspect only "dateToday.isAfter(lastModifiedDate.get("lastUpdated"))", I got,
java.lang.IllegalArgumentException: Partial cannot be null at org.joda.time.base.AbstractPartial.isAfter(AbstractPartial.java:351) at org.theorem.scan.qa.util.TestDate.main(TestDate.java:25)
But if I inspect complete if statement, lastModifiedDate.isEmpty() || dateToday.isAfter(lastModifiedDate.get("lastUpdated")), I'm getting "TRUE". If this IF statement yielded Exception, I should have provided separate if conditions with the same assignment inside each of them, lastModifiedDate.put("lastUpdated", dateToday);.
I'm just astonished, how IllegalArgumentException is handled automatically inside if condition.
Aucun commentaire:
Enregistrer un commentaire