Doc Holiday
Doc Holiday

Reputation: 10254

check if a list has all nulls

I have a list of beans that has properties in it. I'm building 2 subLists to see whats in them and passing to a page.

I need to do a check to see if all the values in either 2 sublist's are all nulls

Java:

beans = dao.getResourceExpended(filter);
List<BigDecimal> scheduledBeans = new ArrayList<BigDecimal>();
List<BigDecimal> realBeans = new ArrayList<BigDecimal>();
for (ResourceBean f : beans)
{
    scheduledBeans.add(f.getScheduledResource());
    realBeans.add(f.getRealResource());
}

request.setAttribute("scheduledBeans", scheduledBeans);
request.setAttribute("realBeans", realBeans);

JSTL:

<c:choose>
    <c:when test="${empty scheduledBeans}">
        alert("scheduledBeans Empty");
    </c:when>
    <c:when test="${empty realBeans}">
        alert("realBeans Empty");
    </c:when>
</c:choose>

The "choose" above does not work because the values come back as:

alert("scheduledBeans =" +  scheduledBeans);
alert("realBeans =" +  realBeans);

Upvotes: 1

Views: 116

Answers (1)

BalusC
BalusC

Reputation: 1108537

Just don't add null values to the list.

Replace

scheduledBeans.add(f.getScheduledResource());
realBeans.add(f.getRealResource());

by

if (f.getScheduledResource() != null) {
    gescheduledBeans.add(f.getScheduledResource());
}
if (f.getRealResource() != null) {
    realBeans.add(f.getRealResource());
}

If they are all null, then the list stays empty and then the empty test will pass.

Upvotes: 1

Related Questions