Bug 120 : SqlSelect java.lang.NullPointerException
Priority 
High
Reported Version 
 
Logged By 
littleJava
Status 
Fixed
Fixed Version 
2.0.0
Assigned To 
 
Product 
Ebean - core
Duplicate Of 
 
Created 
08/06/2009
Updated 
08/06/2009
Type 
Bug
 
Attachments 
No attachments

The there is a bug in @SqlSelect:
It always throw the NullPointerException.
The reason is that the code in the DeploySqlSelect.buildSql(CQueryPredicates predicates, OrmQueryRequest request) dosn't check the var dbWhere whether is NULL.


Source???
Table???
Create table user(user_id);

Code???
@Entity
@Sql(
select= {
@SqlSelect(debug=true,query="select user.user_id as userid,count(*) as total from user group by user.user_id")
}
)
public class SqlSelectDemo {
Integer userid;
int total;
public Integer getUserid() {
return this.userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public int getTotal() {
return this.total;
}
public void setTotal(int total) {
this.total = total;
}
}


Run???
Query query= Ebean.createQuery(SqlSelectDemo.class);
List ss = query.findList();

Result???
??????: Error with cn.helphope.dao.pojo.SqlSelectDemo query:
null
Exception in thread "main" javax.persistence.PersistenceException: java.lang.NullPointerException
at com.avaje.ebean.server.query.RawSqlSelectClauseBuilder.build(RawSqlSelectClauseBuilder.java:106)
at com.avaje.ebean.server.query.CQueryBuilder.buildQuery(CQueryBuilder.java:139)
at com.avaje.ebean.server.query.CQueryEngine.findMany(CQueryEngine.java:76)
at com.avaje.ebean.server.query.DefaultOrmQueryEngine.findMany(DefaultOrmQueryEngine.java:95)
at com.avaje.ebean.server.core.OrmQueryRequest.findList(OrmQueryRequest.java:215)
at com.avaje.ebean.server.core.DefaultServer.findList(DefaultServer.java:1053)
at com.avaje.ebean.query.DefaultOrmQuery.findList(DefaultOrmQuery.java:491)
at cn.helphope.dao.SqlSelectDemoDao.main(SqlSelectDemoDao.java:21)
Caused by: java.lang.NullPointerException
at com.avaje.ebean.server.deploy.DeploySqlSelect.buildSql(DeploySqlSelect.java:199)
at com.avaje.ebean.server.query.RawSqlSelectClauseBuilder.build(RawSqlSelectClauseBuilder.java:88)
... 7 more


Reason???
When I debug the ebean source , I found there is a logic error in the class DeploySqlSelect :
public String buildSql(CQueryPredicates predicates, OrmQueryRequest request) {
??????
String dbWhere = predicates.getDbWhere();
if (dbWhere.length() > 0) {
??????
}
??????
}


If the var dbWhere is null , the NullPointerException will be throwed.So it is better that :
public String buildSql(CQueryPredicates predicates, OrmQueryRequest request) {
??????
String dbWhere = predicates.getDbWhere();
if(dbWhere != null) {
if (dbWhere.length() > 0) {
??????
}
}

 
Rob 08 Jun 07:50
Fixed in HEAD

Quite right. Added in the test to make sure dbWhere != null.

Change now in HEAD.

woResponse

Upload a file