【问题标题】:A "NullPointerException" could be thrown; "setCredentials()" can return null可能会抛出“NullPointerException”; “setCredentials()”可以返回 null
【发布时间】:2018-11-15 22:40:08
【问题描述】:

为什么 Sonar 将此标记为可能的 NullPointerException?

public void credentialSetter(@Headers Map<String, Object> headersMap) {

    SoapHeader uName = new SoapHeader(new QName(NAMESPACE_URL, USERNAMETOKEN), 
            setCredentials(USERNAMETOKEN, this.username).getDocumentElement());

    SoapHeader pTxt = new SoapHeader(new QName(NAMESPACE_URL, P), 
            setCredentials(P, this.pas).getDocumentElement());

两次都以“setCredentials”为单位,我尝试用 if 语句包围它以检查它是否不为空,还尝试在实际方法中检查它是否为空,以涵盖所有基础。

private Document setCredentials(String credential, String value) {
    StringWriter writer = new StringWriter();
    JAXBContext context;
    try {
        if (null != credential && null != value) {
            context = JAXBContext.newInstance(value.getClass());
            QName qName = new QName(NAMESPACE_URL, credential);
            JAXBElement<String> root = new JAXBElement<>(qName, String.class, value);
            context.createMarshaller().marshal(root, writer);
            return DocumentBuilderFactory.newInstance().newDocumentBuilder()
                    .parse(new InputSource(new StringReader(writer.toString())));
        }
    } catch (Exception e) {
        LOG.error("Error converting {} to XML {}", credential, e);
    }
    return null;
}

【问题讨论】:

  • 我猜是因为setCredentials可以return null
  • @DamCx 我明白了,但它(希望)永远不会返回 null 并且 return 语句只是为了完成该方法,我可以尝试什么来代替它?
  • @DamCx 如果出现异常或凭证或值为 null,则返回 null。你能做什么取决于如果你在这里遇到异常应该发生什么:抛出运行时异常可能是一个选项。
  • 我还建议不要捕获异常的基类。只捕获已检查的异常

标签: java nullpointerexception sonarqube


【解决方案1】:

setCredentials() 的最后一行返回 null,因此以下两行可能会抛出 NullPointerException。将您的逻辑包装在空检查中并没有真正帮助,因为它仍然有可能返回 null

SoapHeader uName = new SoapHeader(new QName(NAMESPACE_URL, USERNAMETOKEN), 
        setCredentials(USERNAMETOKEN, this.username).getDocumentElement());

SoapHeader pTxt = new SoapHeader(new QName(NAMESPACE_URL, P), 
        setCredentials(P, this.pas).getDocumentElement());

解决方案是使用以下方法

SoapHeader uName;
Document document = setCredentials(USERNAMETOKEN, this.username);
if (document != null) {
    uName = new SoapHeader(new QName(NAMESPACE_URL, USERNAMETOKEN), 
            document.getDocumentElement());
} else {
    // whatever you need to do
}

【讨论】:

    【解决方案2】:

    您正在对setCredentials's 返回值调用getDocumentElement(),返回值可以是null。如果是这样的话,你会得到一个例外。这就是 Sonar 警告您的原因。

    好的,你已经用 try-catch 封装了 setCredentials 的主要逻辑,但你仍然返回 null 以防出错。

    一般来说,您可以执行以下操作:

    Document credentials = setCredentials(USERNAMETOKEN, this.username);
    if (null != credentials){
        SoapHeader uName = new SoapHeader(new QName(NAMESPACE_URL, USERNAMETOKEN), 
                credentials.getDocumentElement());
    }
    

    【讨论】:

      【解决方案3】:

      您的 setCredentials() 方法可以返回 null :return null;

      Sonar 正在检测这一点 + 您使用返回的对象到 .getDocumentElement() 的事实,这将在 NPE 中推断,因为它是一个空引用。

      如果你想覆盖它(不建议这样做)返回一个新的 Document 实例而不是 null (假设方法 newInstance() 实际上返回一个新实例):

      private Document setCredentials(String credential, String value) {
      StringWriter writer = new StringWriter();
      JAXBContext context;
      try {
          if (null != credential && null != value) {
              context = JAXBContext.newInstance(value.getClass());
              QName qName = new QName(NAMESPACE_URL, credential);
              JAXBElement<String> root = new JAXBElement<>(qName, String.class, value);
              context.createMarshaller().marshal(root, writer);
              return DocumentBuilderFactory.newInstance().newDocumentBuilder()
                      .parse(new InputSource(new StringReader(writer.toString())));
          }
      } catch (Exception e) {
          LOG.error("Error converting {} to XML {}", credential, e);
      }
      return DocumentBuilderFactory.newInstance();
      

      }

      【讨论】:

        猜你喜欢
        • 2018-06-20
        • 2021-06-06
        • 2021-03-12
        • 2023-01-30
        • 2022-08-03
        • 1970-01-01
        • 1970-01-01
        • 2011-03-09
        相关资源
        最近更新 更多