Java:
try {
if (requestModel.getDateTo() != null || !(requestModel.getDateTo().isEmpty())) {
formatter = new SimpleDateFormat("dd/MM/yyyy");
Date dateTo = formatter.parse(requestModel.getDateTo());
requestDOOb.setDateTo(dateTo);
} else {
requestDOOb.setDateTo(dateFrom);
}
}
catch (ParseException e) {
e.printStackTrace();
return "request";
}
Exception:
java.text.ParseException: Unparseable date: "" at requestDOOb.setDateTo(dateFrom);
What should I actually be checking for in terms of the null pointer? The two conditions I provided do not seem to satisfy the exception requirement.
Your logic is incorrect. Use "&&" instead of "||".
if (requestModel.getDateTo() != null || !(requestModel.getDateTo().isEmpty()))
You have to use a &&
(and) instead of ||
(or).
You currently check if date is not null OR if it not empty.
So for an empty string you get (true || false) which is true and therefor it tries to parse the empty date.
Here:
if (requestModel.getDateTo() != null || !requestModel.getDateTo().isEmpty())
This will always evaluate both expressions.
You want to use && instead; to have that implicit guarantee that the first expression evaluated to true before making the second test.
|| has to evaluate both expressions (when the first one is false ); whereas && will not evaluate the second expression when the first one is false .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.