The line in question is return pFile.exists() ? true : null;
. As it does not raise any compilation error, what is the explanation for this. It ended up raising NPE
.
import java.io.File;
public class Main {
public static void main(String... args) {
boolean accept = accept(new File(""));
System.out.println("accept = " + accept);
}
public static boolean accept(File pFile) {
System.out.println(pFile.exists()); // prints: false, so pFile is not null
return pFile.exists() ? true : null; //this line should throw compilation error
}
}
pFile
is not null
; a File
is instantiated as you can see. But obviously the file is not there. The question is not about pFile
. I am interested in how the operator is dealing with null
.