Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java A「;NullPointerException“;可以扔掉&引用;setCredentials();可以返回null_Java_Nullpointerexception_Sonarqube - Fatal编程技术网

Java A「;NullPointerException“;可以扔掉&引用;setCredentials();可以返回null

Java A「;NullPointerException“;可以扔掉&引用;setCredentials();可以返回null,java,nullpointerexception,sonarqube,Java,Nullpointerexception,Sonarqube,为什么声纳将此标记为可能的NullPointerException public void credentialSetter(@Headers Map<String, Object> headersMap) { SoapHeader uName = new SoapHeader(new QName(NAMESPACE_URL, USERNAMETOKEN), setCredentials(USERNAMETOKEN, this.username).

为什么声纳将此标记为可能的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());
public void credentialSetter(@Headers-Map-Headers-Map){
SoapHeader uName=新的SoapHeader(新的QName(名称空间\ URL,用户名令牌),
setCredentials(USERNAMETOKEN,this.username).getDocumentElement());
SoapHeader pTxt=新的SoapHeader(新的QName(名称空间\ URL,P),
setCredentials(P,this.pas).getDocumentElement());
两次都是“setCredentials”,我试着用if语句来检查它是否为null,也试着在实际方法中检查它是否为null,以覆盖所有的基

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;
}
私有文档集凭据(字符串凭据,字符串值){
StringWriter编写器=新的StringWriter();
JAXBContext上下文;
试一试{
if(null!=凭证&&null!=值){
context=JAXBContext.newInstance(value.getClass());
QName QName=新的QName(名称空间\ URL,凭证);
JAXBElement root=newjaxbelement(qName,String.class,value);
context.createMarshaller().marshal(root,writer);
返回DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(新的InputSource(新的StringReader(writer.toString()));
}
}捕获(例外e){
error(“将{}转换为XML{}时出错”,凭证,e);
}
返回null;
}

您正在调用
setCredentials的
返回值上的
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());
}

setCredentials()
的最后一行返回null,因此下面两行可能抛出
NullPointerException
。将逻辑包装在null检查中并没有真正的帮助,因为它仍然有返回
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
}
您的
setCredentials()
方法可以返回null:
returnnull;

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

如果要覆盖此选项(不建议这样做),请返回Document的新实例,而不是null(假设方法newInstance()实际返回一个新实例):

私有文档集凭据(字符串凭据,字符串值){
StringWriter编写器=新的StringWriter();
JAXBContext上下文;
试一试{
if(null!=凭证&&null!=值){
context=JAXBContext.newInstance(value.getClass());
QName QName=新的QName(名称空间\ URL,凭证);
JAXBElement root=newjaxbelement(qName,String.class,value);
context.createMarshaller().marshal(root,writer);
返回DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(新的InputSource(新的StringReader(writer.toString()));
}
}捕获(例外e){
error(“将{}转换为XML{}时出错”,凭证,e);
}
返回DocumentBuilderFactory.newInstance();

}

我想这是因为setCredentials可以
返回null
@DamCx,我明白了,但它会(希望如此)永不返回null,返回语句只是为了完成方法,我可以尝试什么来代替它?@DamCx如果出现异常或凭据或值为null,则返回null。您可以执行的操作取决于如果在此处遇到异常,将发生什么:抛出运行时异常可能是一个选项。我还建议不要捕获t异常的基类。仅捕获已检查的异常
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();