Does java.util.List.isEmpty() check if the list itself is null, or do I have to do this check myself?
For example:
List<String> test = null;
if (!test.isEmpty()) {
for (String o : test) {
// do stuff here
}
}
Will this throw a NullPointerException because test is null?
You’re trying to call the
isEmpty()method on anullreference (asList test = null;). This will surely throw aNullPointerException. You should doif(test!=null)instead (checking fornullfirst).The method
isEmpty()returns true, if anArrayListobject contains no elements; false otherwise (for that theListmust first be instantiated that is in your case isnull).You may want to see this question.