处理一个返回的对象时,经常会出现
NullPointerException,怎么来避免这种情况,防止内存泄露,导致应用出现漏洞那?
以上的情况具体处理方案:
详解:当从数据源中获取需要的对象时,若对象返回null时,作null情况的逻辑处理,并实例化一个空对象即可。
解决方案:
// 获取对象列表,红色标注的方法有可能返回空对象,即可能出现NullPointerException
List<XOrdPromotionAccessBean> allBeanList = SNPromotionResultCache
.getInstance(getCommandContext()).getOrderPromotions(
abOrder.getOrderIdInEJBType());
以上的情况具体处理方案:
// 返回查询后的对象列表,注:标注绿色代码
public List getOrderPromotions(Long aOrderId) {
List orderPromotions = (ArrayList)ordpromotionCache.get(aOrderId);
if(orderPromotions == null){
orderPromotions = new ArrayList();
ordpromotionCache.put(aOrderId, orderPromotions);
}
return orderPromotions;
}
详解:当从数据源中获取需要的对象时,若对象返回null时,作null情况的逻辑处理,并实例化一个空对象即可。