8 Answers
8
Reset to default
Introducing: Trending sort
You can now cull to sort by
Trending, which boosts votes that take happened recently, helping to surface more than upwards-to-engagement answers.
Trending is based off of the highest score sort and falls back to information technology if no posts are trending.
You lot’re trying to call the
isEmpty()
method on a
null
reference (as
List examination = cypher;
). This will surely throw a
NullPointerException
. You should do
if(test!=nix)
instead (checking for
null
first).
The method
isEmpty()
returns true, if an
ArrayList
object contains no elements; simulated otherwise (for that the
List
must first exist instantiated that is in your case is
null
).
You may want to see this question.
answered
Jul sixteen, 2012 at 20:34
LionKing of beasts
18.2k
22 aureate badges
78 silvery badges
106 bronze badges
noraj
ii,915
1 gold badge
24 silver badges
35 statuary badges
answered
Mar 26, 2013 at eleven:23
cschaefercschaefer
one,575
2 gold badges
10 silver badges
nine bronze badges
1
No,
java.util.List.isEmpty()
doesn’t check if a list is
nothing
.
If y’all are using the Spring framework you can use the
CollectionUtils
class to check if a list is empty or non. It also takes care of the
zip
references. Post-obit is the code snippet from Leap framework’s
CollectionUtils
class.
public static boolean isEmpty(Collection<?> collection) { return (collection == zilch || drove.isEmpty()); }
Even if you lot are not using Bound, you can proceed and tweak this code to add together in your
AppUtil
class.
answered
Aug 25, 2016 at 3:30
Abdullah KhanAbdullah Khan
10.9k
vi golden badges
60 argent badges
71 bronze badges
This
will
throw a
NullPointerException
– as volition any attempt to invoke an case method on a
nil
reference – only in cases like this you lot should make an explicit cheque confronting
zero
:
if ((exam != nada) && !test.isEmpty())
This is much better, and clearer, than propagating an
Exception
.
answered
Jul 16, 2012 at xx:33
pb2qpb2q
56.7k
eighteen gold badges
144 argent badges
145 statuary badges
1
-
For a List, I sometimes use: if ( eList != null && eList.size() > 0 )
November 26, 2013 at 19:26
Invoking any method on any null reference will ever event in an exception. Exam if the object is null first:
List<Object> test = zilch; if (exam != null && !examination.isEmpty()) { // ... }
Alternatively, write a method to encapsulate this logic:
public static <T> boolean IsNullOrEmpty(Collection<T> list) { return listing == null || list.isEmpty(); }
And so you can exercise:
Listing<Object> examination = null; if (!IsNullOrEmpty(examination)) { // ... }
answered
Jul 16, 2012 at 20:33
cdhowiecdhowie
146k
22 gold badges
274 silver badges
286 bronze badges
two
-
“Does java.util.List.isEmpty() cheque if the list itself is null?”
seems pretty articulate to me — he is asking about the nullity of the list, not its contents.
Jul 16, 2012 at 20:37
-
Yeah, he totally dislocated me with such a strange question. I mean, the other way would also exist dumb, because the check is for
if(!empty) and then iterate
… But I deleted my stupid comment, before I saw your answer. Maybe he comes from PHP where we have
!empty($foo)
as somewhat an alias for
isset($foo) && $foo != ""
.
Jul 16, 2012 at 20:39
In addition to Lion’s answer, I can say that you better use
if(CollectionUtils.isNotEmpty(exam)){...}
.
This also checks for null, so a manual cheque is non needed.
answered
Jul 6, 2015 at 9:09
lxknvlklxknvlk
ii,494
one gilded badge
23 silver badges
thirty bronze badges
Yes, it will
throw an Exception. Possibly you lot are used to
PHP
code, where
empty($element)
does also cheque for
isset($element)
. In Java this is not the case.
Y’all can memorize that hands, considering the method is direct called on the list (the method belongs to the list). And so if there is no listing, and then there is no method. And Coffee will mutter that there is no listing to call this method on.
answered
Jul 16, 2012 at xx:33
aufziehvogelaufziehvogel
7,027
v gold badges
33 silver badges
53 statuary badges
Yous can employ your own isEmpty (for multiple collection) method as well. Add this to your Util class.
public static boolean isEmpty(Collection... collections) { for (Drove collection : collections) { if (null == drove || collection.isEmpty()) return true; } return false; }
answered
Jun 16, 2017 at vii:42
Arif AcarArif Acar
i,282
2 gold badges
18 silver badges
29 bronze badges
3
-
Maybe yous ought to rename information technology to
areEmpty()
? I detect such methods/functions harder to utilise because they let some stuff slip through. Also, if the offset
Collection
is null or empty and the rest are neither, you’d get an unexpected behavior.
Sep eight, 2018 at 22:56
-
What is the “Util class”? An existing course? Meant as a common noun? Something else?
Feb xvi at 17:18
-
containsEmpty
would be the more appropriate name
Apr 22 at 23:43
Not the answer you’re looking for? Browse other questions tagged java list or enquire your own question.
Source: https://stackoverflow.com/questions/11512034/does-java-util-list-isempty-check-if-the-list-itself-is-null
Apache Utills are absolutely not bad! Recently I discovered SpringUtils.join – very useful using on collections. Sorry for a niggling offtop 🙂
Apr 10, 2014 at 12:30