【问题标题】:A "NullPointerException" could be thrown; "activationConfigParser" is nullable here可能会抛出“NullPointerException”; \"activationConfigParser\" 在这里可以为空
【发布时间】:2023-01-30 04:48:21
【问题描述】:

声纳问题

private void getGtcj(String gtcjStatusValue, String strArchiveReqd) throws Exception {
    XPathHelper activationConfigParser = null;
    try {
        activationConfigParser = ConfigUtil.getInstance().getConfigParser(new URL((V21Constants.FILE
                + System.getProperty(V21Constants.USER_DIR) + "/vServe21/config/ActivationParameters.xml")));
    } catch (Exception e) {
        log.error(e.getMessage());
    }

    StringBuffer useGTCJSolution = new StringBuffer();
    useGTCJSolution.append(XPathConstants.ACTIVATION_CONFIG_ACTIVATION_PARAM)
            .append("/parameter[@name = '").append(XPathConstants.TAG_NAME_USE_GTCJ_SOLUTION)
            .append("']");

    String useGTCJSolutionStr = activationConfigParser.evaluateXPath(useGTCJSolution.toString());
    log.debug("useGTCJSolutionStr value:" + useGTCJSolutionStr);

    if (useGTCJSolutionStr != null && useGTCJSolutionStr.trim().equalsIgnoreCase(V21Constants.YES)
            && (gtcjStatusValue.equalsIgnoreCase(Statuses.ACTIVATION_SUCCESS)
                    || gtcjStatusValue.equalsIgnoreCase(Statuses.ROLLBACK_SUCCESS)
                    || gtcjStatusValue.equalsIgnoreCase("Rollback Failure"))) {
        log.debug("No need to archive and send response from here.");
    } else {
        log.debug("inside GTCJSolution for GTCJ orders...Archiving and sending response xml");
        if (strArchiveReqd != null && "Yes".equalsIgnoreCase(strArchiveReqd)) {
            archiveXML(responseFileName, V21Constants.VIF_ARCHIVE_RESPONSE_XML_PATH);
        }
        // sending the response XML
        response = new Response();
        response.sendResponseXML(properties, responseXml, bNBSConnectivityFlag, queueName, address);
    }
}

我发现在 catch 之后应该有一个 finally 块,但我不知道在 finally 块中添加什么。或者还有其他解决方案吗?

【问题讨论】:

  • 您能否添加收到警告/错误的行?
  • 得到一个“NullPointerException”可能会被抛出; “activationConfigParser”在这里可以为空。
  • 试想如果try块中的代码抛出异常会发生什么:异常将被catch (Exception e) { log.error(e.getMessage()); }块捕获,但此时activationConfigParser仍然是null,然后activationConfigParser.evaluateXPath(...)抛出NullPointerException
  • 你为什么在那里捕获那个异常?当 try 块内的代码实际抛出此异常时,您期望整个方法有什么行为?

标签: java nullpointerexception sonarqube


【解决方案1】:

当您创建变量 activationCOnfigParser 时,您处于 try/Catch 块中。您可以绕过此错误:

private void getGtcj(String gtcjStatusValue, String strArchiveReqd) throws Exception {
XPathHelper activationConfigParser = null;
try {
    activationConfigParser = ConfigUtil.getInstance().getConfigParser(new URL((V21Constants.FILE
            + System.getProperty(V21Constants.USER_DIR) + "/vServe21/config/ActivationParameters.xml")));
} catch (Exception e) {
    actionConfigParser = <DEFAULT VALUE>
    log.error(e.getMessage());

}

在 catch 块中,您可以替换为 ConfigParser 在出现异常时必须采用的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-06
    • 2021-03-12
    • 1970-01-01
    • 2018-11-15
    • 2021-06-18
    • 2018-06-20
    相关资源
    最近更新 更多