mercoledì 17 dicembre 2008

Java 5 advanced for loop and null values

A quick note on an argument that may be obvious, but I never run through until today.
The advanced for loop (the for(Type t : Collection) form), introduced in Java 5 doesn't check for null collections. What this means is that if you pass to your loop a null collection (possible, when you don't have total control on the arguments that are passed to your method), the for loop will throw a NullPointerException. This is a correct behavior, since usually you don't want to consider in the same way null collections and empty collections, but can be a source of errors (possibly runtime errors...) if you don't take care of null values by yourself.

Nessun commento: