java 输入参数可以为空_java-声纳问题-参数必须为非空但被标记为可为空

我已经写了这个谓词,而声纳对此有所抱怨.我不确定如何解决此违规问题.请帮忙:

import com.google.common.base.Predicate;

import java.util.Map;

public final class FooPredicate {

private FooPredicate(){}

public static Predicate> isFirstElement(final Long o) {

return new Predicate>() {

@Override

public boolean apply(Map.Entry foo) {

return foo.getKey().equals(o);

}

};

}

}

它正在抱怨apply方法的Foo参数.

06001

apply方法定义为采用可为null的值,对此我无能为力.声纳和番石榴不是在这里战斗吗?

> boolean apply(@Nullable

> T input)

>

> Returns the result of applying this predicate to input. This method is

> generally expected, but not absolutely required, to have the following

> properties:

>

> Its execution does not cause any observable side effects.

> The computation is consistent with equals; that is, Objects.equal(a, b) implies that predicate.apply(a) ==

> predicate.apply(b)).

>

> Throws:

> NullPointerException - if input is null and this predicate does not accept null arguments

解决方法:

您正在做foo.getKey().如果foo为null,则将抛出NullPointerException,但您并不是在声明foo不能为null.

在使用前请先检查foo.

if (foo != null) {

return foo.getKey().equals(o);

} else {

return null;

}

标签:collections,sonarqube,guava,java

来源: https://codeday.me/bug/20191028/1955965.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值