Java – Checking if an Object is null at the same time as a value of one of it’s fields


Which of these would be correct?

if(dialog != null && dialog.isShowing){}

if(dialog.isShowing && dialog != null){}

if(dialog != null){

Best Solution

The first and third ones are both OK to use because they won't process past the null check. The second one can result in a NullPointerException because it's referencing dialog before you've checked if it's null.