Tuesday, May 22, 2018

Groovy double blade: safe navigation operator

In groovy, safe navigation operator is a very cool feature. It helps avoid NullPointerException. However, when using it, need to be careful and avoid something like
list?.size()++
There is no issue for the first part list?.size() at all, the safe navigation operator does it job and helps avoid NullPointerException. The issue is the ++ part. Suppose list is null, list?.size() will also be null. Then null++ will generate an error
java.lang.NullPointerException: Cannot invoke method next() on null object”.


No comments:

Post a Comment