My ternary operator is throwing a NullPointerException
even though I explicitly check if the value of my list is null
. However, if I add parenthesis around the ternary operator, my code works.
Code without parenthesis (throwing the exception):
final List<String> listData = null;
System.out.println("Insert new data (Created Monitorings) : " +
listData != null ? listData.size() : 0);
Code with parenthesis (working fine):
final List<String> listData = null;
System.out.println("Insert new data (Created Monitorings) : " +
(listData != null ? listData.size() : 0));
Can somebody explain it how exactly it is working.