Does java.util.List.isEmpty() check if the list itself is null?
JavaListJava Problem Overview
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
?
Java Solutions
Solution 1 - Java
You're trying to call the isEmpty()
method on a null
reference (as List test = null;
). This will surely throw a NullPointerException
. You should do if(test!=null)
instead (checking for null
first).
The method isEmpty()
returns true, if an ArrayList
object contains no elements; false otherwise (for that the List
must first be instantiated that is in your case is null
).
You may want to see this question.
Solution 2 - Java
I would recommend using Apache Commons Collections:
which implements it quite ok and well documented:
/**
* Null-safe check if the specified collection is empty.
* <p>
* Null returns true.
*
* @param coll the collection to check, may be null
* @return true if empty or null
* @since Commons Collections 3.2
*/
public static boolean isEmpty(Collection coll) {
return (coll == null || coll.isEmpty());
}
Solution 3 - Java
No, java.util.List.isEmpty()
doesn't check if a list is null
.
If you are using the Spring framework you can use the CollectionUtils
class to check if a list is empty or not. It also takes care of the null
references. Following is the code snippet from Spring framework's CollectionUtils
class.
public static boolean isEmpty(Collection<?> collection) {
return (collection == null || collection.isEmpty());
}
Even if you are not using Spring, you can go on and tweak this code to add in your AppUtil
class.
Solution 4 - Java
This will throw a NullPointerException
- as will any attempt to invoke an instance method on a null
reference - but in cases like this you should make an explicit check against null
:
if ((test != null) && !test.isEmpty())
This is much better, and clearer, than propagating an Exception
.
Solution 5 - Java
Invoking any method on any null reference will always result in an exception. Test if the object is null first:
List<Object> test = null;
if (test != null && !test.isEmpty()) {
// ...
}
Alternatively, write a method to encapsulate this logic:
public static <T> boolean IsNullOrEmpty(Collection<T> list) {
return list == null || list.isEmpty();
}
Then you can do:
List<Object> test = null;
if (!IsNullOrEmpty(test)) {
// ...
}
Solution 6 - Java
Yes, it will throw an Exception. Maybe you are used to PHP code, where empty($element)
does also check for isset($element)
. In Java this is not the case.
You can memorize that easily, because the method is directly called on the list (the method belongs to the list). So if there is no list, then there is no method. And Java will complain that there is no list to call this method on.
Solution 7 - Java
In addition to Lion's answer, I can say that you better use if(CollectionUtils.isNotEmpty(test)){...}
.
This also checks for null, so a manual check is not needed.
Solution 8 - Java
You can use your own isEmpty (for multiple collection) method too. Add this to your Util class.
public static boolean isEmpty(Collection... collections) {
for (Collection collection : collections) {
if (null == collection || collection.isEmpty())
return true;
}
return false;
}