gpt4 book ai didi

java - 如何处理调用 null 返回方法的方法中的 null 值?

转载 作者:行者123 更新时间:2023-12-02 07:40:34 25 4
gpt4 key购买 nike

这是示例代码

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() 方法中处理它呢?是否需要进行一些异常处理?如果是这样,怎么办?

最佳答案

进行 null 检查,然后使用 message 抛出 RuntimeException

 if(checkAssetRole==null)
throw new RuntimeException ("AssetRole value is NULL.");

你的代码看起来像这样。

String checkAssetRole = doCheckAssetRole(savingObject);

if(checkAssetRole==null)
throw new RuntimeException ("AssetRole value is NULL.");

if (checkAssetRole != null && !checkAssetRole.equals("MissingAssetRole")) {
return new OperationResult(new OperationExecutionError("SIP-37006",
new String[] {"Duplicate asset roles have been defined: " + checkAssetRole},
getLocalizationGate()));
}

关于java - 如何处理调用 null 返回方法的方法中的 null 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11666590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com