【发布时间】:2015-04-13 05:08:23
【问题描述】:
我有方法
public static List<Transaction> getTransactions(){
if (ShoppingSessionDao.getCurrentShoppingSession()==null){
//what return?
}
return new Select().from(Transaction.class).where("shoppingSession = ?",
ShoppingSessionDao.getCurrentShoppingSession().getId()).execute();
}
它返回给我数据列表。
那我做
private void setData() {
transactionList = TransactionDao.getTransactions();
baseAdapter = new BasketAdapter(transactionList);
gridView.setAdapter(baseAdapter);
}
但有时(如果 ShoppingSession 为空)ShoppingSessionDao.getCurrentShoppingSession()return me NULL。
我该如何处理这个错误?
解决方案
private void setData() {
ShoppingSession shoppingSession = ShoppingSessionDao.getCurrentShoppingSession();
if (shoppingSession==null){
return;
}
transactionList = TransactionDao.getTransactions(shoppingSession);
baseAdapter = new BasketAdapter(transactionList);
gridView.setAdapter(baseAdapter);
tvPrice.setText(calculateTotalSum());
}
和
public static List<Transaction> getTransactions(ShoppingSession shoppingSession){
return new Select().from(Transaction.class).where("shoppingSession = ?",
shoppingSession.getId()).execute();
}
【问题讨论】:
-
添加try catch异常
-
@"什么回报?"返回null并在调用者方法中处理它?这甚至是一个“好”的问题吗? How do I ask a good question?