이 샘플 코드 조각Null 반환 메서드가 호출되는 메서드에서 null 값을 처리하는 방법은 무엇입니까?
public OperationResult beforeEverything(BDDObject savingObject) {
String checkAssetRole = doCheckAssetRole(savingObject);
if (checkAssetRole != null && !checkAssetRole.equals("MissingAssetRole")) {
return new OperationResult(new OperationExecutionError("SIP-37006",
new String[] {"Duplicate asset roles have been defined: " + checkAssetRole},
getLocalizationGate()));
}
ArrayList<String> warnings = new ArrayList<String>();
boolean showWarning = false;
if (checkAssetRole != null && checkAssetRole.equals("MissingAssetRole")) {
mLogger.debug("Warning of Asset role");
warnings.add(new String(
"Asset role is missing. Do you want to save the record?"));
showWarning = true;
}
return OperationResult.OK;
}
것은이 doCheckAssetRole
방법은 null
을 반환이다. 어떻게 beforeEverything()
메서드에서 처리 할 수 있습니다. 할 몇 가지 예외 처리가 있습니까? 그렇다면 어떻게?
선택'IllegalArgumentException'입니다 이 경우에는 RuntimeException보다 더 좋다. 이것을 강조 표시하려면 – assylias
+1. 그것의 더 특정한. – amicngh
1. 다음 if 조건에서도 null 체크를 제거해야합니다. 더 이상 필요하지 않기 때문입니다. 2. IllegalArgumentException은 더 구체적이지 않습니다 - 모든 오류가 불법 인수로 인한 것이 아니기 때문에 생각합니다. 삼.checkAssetRole에 대한 null 체크를 모두 제거하면 NullPointerException이 발생합니다. 이제 이것이 내가 부르는 것입니다. – Axel