Things To Remember - NullPointerException on if with null boolean value
- access_time 6 years ago
- date_range 18/04/2013
- comment 0
- share 3
- label_outlineJava
Update 12/2017: Could just smile at this now.
This one occurred to me today and it's caused by laziness I got used to while coding in untyped languages :) The condition in if
statement should evaluate to boolean. If it's null
, then NullPointerException
is thrown as in the following test case.
@Test(expected = NullPointerException.class)
public void shouldThrowNullPointerException() throws Exception {
Boolean someFlag = null;
if (someFlag) {
// unexpected
} else {
// could be expected
}
}