Although you generally compare Java objects using the equals
method, null
comparison is a notable exception: no Java Object
compares equal
to null
- Java classes need to satisfy this requirement:
For any non-null reference value x, x.equals(null)
should return false
.
Moreover, file
is null
to start with, so calling any methods on it, including equals
, will result in NPE.
Therefore, you need to use reference equality instead:
while (file == null) {
...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…