我无法处理错误?
I can not handle error?
我有方法
public static List<Transaction> getTransactions(){
if (ShoppingSessionDao.getCurrentShoppingSession()==null){
//what return?
}
return new Select().from(Transaction.class).where("shoppingSession = ?",
ShoppingSessionDao.getCurrentShoppingSession().getId()).execute();
}
它return我用数据列出。
那我做
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 围绕您的代码:
try {
ShoppingSession shoppingSession = ShoppingSessionDao.getCurrentShoppingSession();
transactionList = TransactionDao.getTransactions(shoppingSession);
baseAdapter = new BasketAdapter(transactionList);
gridView.setAdapter(baseAdapter);
tvPrice.setText(calculateTotalSum());
} catch (NullPointerException ex) {
Log.e("Error: ", ex.getLocalizedMessage());
//Do error handling here
}
另一种方法是使用以下代码扩展您正在使用的当前方法:
ShoppingSession shoppingSession = ShoppingSessionDao.getCurrentShoppingSession();
if (shoppingSession != null) {
transactionList = TransactionDao.getTransactions(shoppingSession);
baseAdapter = new BasketAdapter(transactionList);
gridView.setAdapter(baseAdapter);
tvPrice.setText(calculateTotalSum());
} else {
tvPrice.setText("Shopping session is null");
//Do error handling here
}
我有方法
public static List<Transaction> getTransactions(){
if (ShoppingSessionDao.getCurrentShoppingSession()==null){
//what return?
}
return new Select().from(Transaction.class).where("shoppingSession = ?",
ShoppingSessionDao.getCurrentShoppingSession().getId()).execute();
}
它return我用数据列出。
那我做
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 围绕您的代码:
try {
ShoppingSession shoppingSession = ShoppingSessionDao.getCurrentShoppingSession();
transactionList = TransactionDao.getTransactions(shoppingSession);
baseAdapter = new BasketAdapter(transactionList);
gridView.setAdapter(baseAdapter);
tvPrice.setText(calculateTotalSum());
} catch (NullPointerException ex) {
Log.e("Error: ", ex.getLocalizedMessage());
//Do error handling here
}
另一种方法是使用以下代码扩展您正在使用的当前方法:
ShoppingSession shoppingSession = ShoppingSessionDao.getCurrentShoppingSession();
if (shoppingSession != null) {
transactionList = TransactionDao.getTransactions(shoppingSession);
baseAdapter = new BasketAdapter(transactionList);
gridView.setAdapter(baseAdapter);
tvPrice.setText(calculateTotalSum());
} else {
tvPrice.setText("Shopping session is null");
//Do error handling here
}