在尝试从HashMap获取值时,在Eclipse中出现SonarLint错误。

huangapple go评论33阅读模式
英文:

Getting sonarlint error in eclipse while trying to get value from hashmap

问题

我遇到了错误,类似于**“在访问值之前调用“Optional#isPresent()”**,尝试从哈希映射中获取值。

代码:

String promolevelname = eventCampiagnResponseObj.getEventCampMap().get(Integer.valueOf(sceobj[18].toString())).keySet().stream().findFirst().get();

请给我提供解决此问题的解决方案,谢谢。

英文:

I'm getting error like "Call "Optional#isPresent()" before accessing the value", while trying to get value from hashmap.

Code:

String promolevelname = eventCampiagnResponseObj.getEventCampMap().get(Integer.valueOf(sceobj[18].toString())).keySet().stream().findFirst().get();

Please give me a solution to resolve this issue, Thanks

答案1

得分: 1

findFirst 返回 Optional,但如果没有值,它会抛出 NoSuchElementException 异常。因此,您需要检查是否存在值。

在您的情况下,应该如下所示:

Optional<String> optionalValue = eventCampiagnResponseObj.getEventCampMap().get(Integer.valueOf(sceobj[18].toString())).keySet().stream().findFirst();
if (optionalValue.isPresent()) {
    String promolevelname = optionalValue.get();
}
英文:

findFirst returns Optional but it will throw NoSuchElementException if there is no value. So you need to check if there is a value present.

Optional&lt;String&gt; value = obj.getOptional()
if (value.isPresent()) {
    value.get();
}

In your situation it should look like this:

Optional&lt;String&gt; optionalValue = eventCampiagnResponseObj.getEventCampMap().get(Integer.valueOf(sceobj[18].toString())).keySet().stream().findFirst();
if (optionalValue .isPresent()) {
    String promolevelname  = optionalValue.get();
}

答案2

得分: 1

代码行建议检查返回的Optional对象是否为null,然后再执行get()方法。

一种可能的解决方案是:

Optional<String> promolevelnameOpt = eventCampiagnResponseObj.getEventCampMap().get(Integer.valueOf(sceobj[18].toString())).keySet().stream().findFirst();
if (promolevelnameOpt.isPresent()) {
  promolevelname = promolevelnameOpt.get();
} else {
  // promolevelnameOpt 包含null
}
英文:

It suggests that the code line

eventCampiagnResponseObj.getEventCampMap().get(Integer.valueOf(sceobj[18].toString())).keySet().stream().findFirst()

is returning an Optional object, which you should check if it's not 'null' and only then execute get() on it.

One possible solution would be this:

Optional&lt;String&gt; promolevelnameOpt = eventCampiagnResponseObj.getEventCampMap().get(Integer.valueOf(sceobj[18].toString())).keySet().stream().findFirst().get();
if (promolevelnameOpt.isPresent()) {
  promolevelname = promolevelnameOpt.get();
} else {
  // promolevelnameOpt contains null
}

答案3

得分: 1

你可以使用.orElse(null)而不是.get()

所以你的代码变成:

String promolevelname = eventCampiagnResponseObj.getEventCampMap().get(Integer.valueOf(sceobj[18].toString())).keySet().stream().findFirst().orElse(null);
英文:

Instead of .get() you could use .orElse(null)

So your code becomes :

String promolevelname = eventCampiagnResponseObj.getEventCampMap().get(Integer.valueOf(sceobj[18].toString())).keySet().stream().findFirst().orElse(null);

huangapple
  • 本文由 发表于 2020年7月29日 23:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63156886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定