Betafish Betafish - 1 month ago 22
Java Question

To check if List<Map<String, String>> is empty or not

The answer from a library (

DBpediaLookupClient.variableBindings()
) results in

List<Map<String, String>>


I am trying to check is if the list if empty or not.
What I have tried so far is

if (dbpedialookup.variableBindings() != null && !dbpedialookup.variableBindings().isEmpty()) {
System.out.println("Results Present);
} else {
System.out.println("No Results");
}


But, on items where the resultant List is []. Its throwing,

Exception in thread "main" java.lang.NullPointerException


How do we check for
null
in such a situation?

Answer

Look like the top object is null so

if (dbpedialookup != null && 
    dbpedialookup.variableBindings() != null &&
   !dbpedialookup.variableBindings().isEmpty()) {