我试图从数据库中提取数据并将其发布到数据表中。
我的控制器如下
public static Result vwFarmerslist(){
UserAccount user = isLoggedIn();
if (user == null || user.username == null ){
return redirect(controllers.routes.Application.login());
} else {
/**
Get needed params
*/
Map<String, String[]> params = request().queryString();
/**Get Total Records*/
Integer iTotalFarmers = Farmers.find.findRowCount();
Logger.info ("Total Farmers: "+iTotalFarmers);
Logger.info("params+ "+params);
String filter = params.get("search[value]")[0];
Logger.info ("Filter : "+filter);
//get page Size
Integer pageSize = Integer.valueOf(params.get("length")[0]);
Integer page = Integer.valueOf(params.get("start")[0]) / pageSize;
/**
*** Get sorting order and column
*/
String sortBy = "name";
String order = params.get("order[0][dir]")[0];
switch (Integer.valueOf(params.get("order[0][column]")[0])) {
case 0 : sortBy = "name"; break;
case 1 : sortBy = "idno"; break;
//case 2 : sortBy = "regions"; break;
case 2 : sortBy = "mobile"; break;
}
/**
* Get data from db
*/
Page<Farmers> vwfarmerslistPage = Farmers.find.where(
Expr.or(
Expr.ilike("name", "%" + filter + "%"),
Expr.or(
Expr.ilike("idno", "%" + filter + "%"),
Expr.ilike("mobile", "%" + filter + "%")
//Expr.ilike("regions", "%" + filter + "%"),
)
)
).orderBy(sortBy + " " + order + ", id " + order)
.findPagingList(pageSize).setFetchAhead(false)
.getPage(page);
Integer iTotalFarmersRecord = vwfarmerslistPage.getTotalRowCount();
/**
* Construct JSON Return Object
*
*/
ObjectNode result = Json.newObject();
result.put("draw", Integer.valueOf(params.get("draw")[0]));
result.put("recordsTotalFarmers",iTotalFarmers);
result.put("recordsFilter",iTotalFarmersRecord);
ArrayNode arrayNode = result.putArray("data");
for (Farmers c : vwfarmerslistPage.getList()){
ObjectNode row = Json.newObject();
row.put("0",c.toString());
row.put("1",c.idno);
row.put("2",c.mobile);
arrayNode.add(row);
Logger.info("Farmers array: " +arrayNode);
}
return ok(result);
}
}
我的模型如下
public class Farmers extends Model {
@Id
public Long id;
@Constraints.Required
public String fname="";
public String mname="";
@Constraints.Required
public String lname="";
@Constraints.Required
public String idno ="";
@Constraints.Required
public String mobile = "";
public String bankaccountno="";
public String bankname="";
@OneToOne
public Regions regions;
@Formats.DateTime(pattern = "yyyy-mm-dd hh:mm:ss")
public Date createdon = new Date();
@OneToOne
public UserAccount user;
@OneToOne
public UserAccount updateduser;
public static Finder<Long, Farmers> find = new Finder<Long, Farmers>(Long.class,Farmers.class);
/**public static Farmers findByIdno(String idno){
return Farmers.find.where().eq("idno",idno).findUnique();
}
public static Map<String, String> options(){
LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
for(Farmers c:Farmers.find.orderBy("idno").findList()){
options.put(String.valueOf(c.id), c.idno);
}
return options;
}*/
public static List<Farmers> findall(){
return find.all();
}
public String toString(){
String name = fname+" "+mname+" "+lname;
return name;
}
}
当我运行代码时,我得到一个空指针异常,如下面的调试所示
play.api.Application$$anon$1: Execution exception[[NullPointerException: null]]
at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.6.jar:2.3.6]
at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.6.jar:2.3.6]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.6.jar:2.3.6]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.6.jar:2.3.6]
at scala.Option.map(Option.scala:145) [scala-library-2.11.1.jar:na]
Caused by: java.lang.NullPointerException: null
at controllers.ContractCtrl.vwFarmerslist(ContractCtrl.java:66) ~[classes/:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$9$$anonfun$apply$9.apply(routes_routing.scala:361) ~[classes/:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$9$$anonfun$apply$9.apply(routes_routing.scala:361) ~[classes/:na]
at play.core.Router$HandlerInvokerFactory$$anon$4.resultCall(Router.scala:264) ~[play_2.11-2.3.6.jar:2.3.6]
at play.core.Router$HandlerInvokerFactory$JavaActionInvokerFactory$$anon$15$$anon$1.invocation(Router.scala:255) ~[play_2.11-2.3.6.jar:2.3.6]
我的农民桌也不是空的。它有两个左右的条目。