Java's null value denotes the absence of an object reference, but it carries no inherent meaning. Code may use it for uninitialized, lazy, unspecified, missing, or empty states, making its interpretation a developer responsibility. section 5.1
Null becomes especially hazardous in these situations:
- Passing null to a variable-arity method can select the array form, leave the parameter array null, and cause a NullPointerException during iteration. section 2.9.1
- Collection interfaces do not reveal whether null elements are supported or whether contains(null) is safe; collection implementations differ. section 8.3
- Streams may carry null intermediate values, but operations returning Optional and collectors such as toMap() or groupingBy() can reject them. section 9.4
Make a varargs intention explicit
printAll(null) passes a null Object[] rather than an array containing one null, so iteration fails. Use printAll((Object) null) for one null element, or printAll((Object[]) null) when a null array reference is intentional. section 2.9.1
Filter before a terminal operation
List<Integer> list = Arrays.asList(null, 1, 2); list.stream().filter(Objects::nonNull).findFirst(); Filtering early avoids findFirst() failing when the first stream element is null. section 9.4
// I want an array containing null printAll((Object)null); // I want just null array reference printAll((Object[])null);
Compare the two explicit casts that distinguish an array containing null from a null array reference. section 2.9.1
Stream.of((Object)null).findFirst()
Use this minimal expression to reproduce findFirst() failure on a null stream element. section 9.4


































