How can I check if List <obj> is null?

I have a list that takes a list from my server. this list will contain whatever the server finds in the ex database.

List<OBJ> lstObj = new Arraylist<OBJ>;

Service.getOBJ(new AsyncCallback<List<OBJ>>(){
    @Override
    public void onFailure(Throwable caught) {
        caught.printStackTrace();
    }

    @Override
    public void onSuccess(List<OBJ> result) {
        //line to check if result is null
    }
});

      

I tried

if(result==null){
}

      

and also tried

if(result.isempty(){
}

      

but it didn't work. the list will be empty if the server does not find any records from the database. all i have to do is check if empty is empty.

+3


source to share


5 answers


Checking that the list is empty and checking whether it is result

zero are two different things.

if (result == null)

      

will see if the value is a result

null reference, that is, it does not belong to any list.

if (result.isEmpty())

      



will see if the value is a result

reference to an empty list ... the list exists, it just doesn't have any elements.

And of course, in cases where you don't know if it can result

be empty or empty, just use:

if (result == null || result.isEmpty())

      

+19


source


Check the number of elements in the resulting list:



if (0==result.size()) {

    // Your code
}

      

+6


source


You will do like this:

if (test != null && !test.isEmpty()) { }

      

This will check for both null and null, that is, if it is not null and not empty, do your processing.

+4


source


You are obviously new to this programming task if you haven't checked your server yet, so I'm trying to figure out what might happen to your server. Depending on your "objects", you may have valid objects, which are data that are meaningless in different ways. For example, you can have String objects with different types of spaces.

This happens on servers that provide responses using PHP and JSP, where pages are assembled using various include mechanisms and there is a gap between them.

+2


source


Below is the code for your code. If you want the denial logic to just change accordingly. As someone also suggests, CollectionUtils only provides utilities that remove such a null LOC check.

result == null || result.isEmpty() 

      

Hope this helps!

0


source







All Articles