我有這種方法。 問題是,當這個條件滿足即使在調用catch塊時也正在執行代碼
if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1)
它進入catch塊這裏
catch (Exception e) {
this.errorText = e.getMessage().toString();
info.setErrorText(this.errorText.toString());
response.setinfo(info);
}
但它畢竟是execuing下一行是
final boolean toProceedorNot = validate(bagList.toArray(new Bag[bagList.size()]))
什麼如果這是符合要求
if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1)
**then directly return the response;**
這是我的方法
public Response getData(Request request) {
Info info = new Info();
Response response = new Response();
String xmlrequest = request.getxmlMessage();
HashMap listMap = new HashMap();
List<Ungar> UngarList = new ArrayList<Ungar>();
List<Bag> bagList = new ArrayList<Bag>();
UniverseStaxParser xmlparser = new UniverseStaxParser();
try {
listMap = (HashMap) xmlparser.parseData(xmlrequest);
UngarList = (List<Ungar>) listMap.get("UngarItems");
bagList = (List<Bag>) listMap.get("bagItems");
if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1)
throw new Exception("No Valid Data is passed as Input ");
} catch (Exception e) {
this.errorText = e.getMessage().toString();
info.setErrorText(this.errorText.toString());
response.setinfo(info);
}
final boolean toProceedorNot = validate(bagList.toArray(new Bag[bagList.size()]));
try {
if (!toProceedorNot) {
info.setErrorText(errorText);
response.setinfo(info);
} else {
// some logic here goes
}
} catch (Exception e) {
errorText = e.getMessage().toString();
info.setErrorText(errorText);
response.setinfo(info);
}
return response;
}
調用一個空對象的方法會拋出空指針異常。這是一個常見的錯誤。 – Jasonw
非常感謝大家,我修改了代碼,將它保存在一個try塊中,現在它的工作。 – Revathi