如何检查List<SqlRow>是否为空

How to check the List<SqlRow> is empty

我正在使用 Ebean 查询

String sql = " some sql query";

List<SqlRow> row = Ebean.createSqlQuery(sql).findList(); //Ebean return the row list 

if ((row == null) && row.isEmpty()){ //if row is there is no value then allocate 700    
  avgSteps=700;         
}
else {
  for(SqlRow sqlrow : row) {        
     avgSteps = sqlrow.getLong("step");
  }
}   

如果用户在我的 DB 中没有条目,那么它应该 return 一个 null 并将值分配给 avgSteps = 700 但它没有分配值, NullPoinertException 发生。

您需要更改 if 条件如下:

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

您想检查该行是否 为空或为空,而不是 两者 为空和空。

试试这个,

String sql = " some sql query";

List<SqlRow> row = Ebean.createSqlQuery(sql).findList(); //Ebean return the row list 

if ((row.size() == 0)){ //if row is there is no value then allocate 700    
  avgSteps=700;         
}
else {
  for(SqlRow sqlrow : row) {        
     avgSteps = sqlrow.getLong("step");
  }
}