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

时间:2012-07-26 09:43:06

标签: java 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()方法中处理它呢?是否有一些异常处理要做?如果是这样,怎么样?

4 个答案:

答案 0 :(得分:1)

进行null检查,然后使用RuntimeException投放message

 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()));
}

答案 1 :(得分:1)

如果doCheckAssetRole为null,则取决于您的业务逻辑:

  • 它对应一个有效的业务用例,所以你有一个相应的OperationResult对象来返回这种情况,所以你会做类似的事情

    if (checkAssetRole == null)
      return new NoAssetRoleOperationResult();
    

    调用代码将处理这种结果。

  • 它永远不会发生,除非用户犯了错误,然后你抛出一个检查过的异常。

    if (checkAssetRole == null)
      throw new NoAssetRoleException(yourMessage);
    

    然后进行方法声明throws NoAssetRoleExeption。然后,调用代码负责将此错误转发回用户。

  • 由于环境错误(远程服务器关闭)而失败,您抛出了未经检查的异常,如RuntimeException,顶级代码将捕获它以指示存在环境故障。

  • 由于开发错误而失败(理论上这段代码永远不会返回null),然后断言:

    assert checkAssetRole != null : "assetRole should not be null"
    

选择你的案子:)

答案 2 :(得分:0)

从方法中返回null是正常做法。呼叫者需要检查它。 在您的情况下,您可以进行检查并采取相应措施。如果你想抛出和异常,那么你可以这样做。

   public OperationResult beforeEverything(BDDObject savingObject) {

     String checkAssetRole = doCheckAssetRole(savingObject);

     if (checkAssetRole == null) {
         // DO SOMETHING HERE. eg. throw new RuntimeException("Asset Role should not be null");
         //   or simply return null;

     }

答案 3 :(得分:0)

这取决于返回null时您想要做什么。如果要在doCheckAssetRole返回null时返回值NotOK,则可以使用此片段。

public OperationResult beforeEverything(BDDObject savingObject) {

  String checkAssetRole = doCheckAssetRole(savingObject);

  if (checkAssetRole == null) {
    return OperationResult.NotOK; // Or something else that indicates to the calling method what happened.
  else {
    if (!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.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;
  }
}