0

SonarQube quality fails with possible NullPointer in the method

When calling the method I'm checking !=null, but not passing through

private Asset findAsset(List<Asset> assets, Long assetId) {
    return assets.parallelStream()
                 .filter(ast -> ast.getAssetId().equals(assetId))
                 .findFirst()
                 .orElse(null);
}

Im trying the below code:

if (findAsset(assetCompositionList, pair.getKey()) != null) {
    assetMap.put(pair.getKey(),
            findAsset(assets, pair.getKey()).getAssetCode());
}

I was already checking !=null in findAsset() method when called.

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
steve
  • 115
  • 1
  • 8
  • Are you sure `pair` isn't null? – GBlodgett Mar 30 '19 at 19:56
  • `findAsset(assets, pair.getKey())` could still be `null`, isn't it? – Naman Mar 30 '19 at 19:58
  • GBlodgett yes pair.key() will not be null – steve Mar 30 '19 at 20:02
  • Please include an [MCVE], including a complete stack trace – GBlodgett Mar 30 '19 at 20:05
  • On which line you are getting warning, in IDE, specifically which method call, since your method is in a single line – Hemant Patel Mar 30 '19 at 20:07
  • Pratapi Hemant Patel A "NullPointerException" could be thrown; "findAsset()" can return null. – steve Mar 30 '19 at 20:10
  • At which line sonar is pointing for possible exception? – Killer Mar 30 '19 at 20:14
  • 2
    @Naman looks like its not a duplicate question. The user not asking what is NPE and how to fix. Actually user asking why SonarQube is marking that code as possible NPE exception. – Killer Mar 30 '19 at 20:24
  • 1
    Error is at line `assetMap.put(pair.getKey(), findAsset(assets, pair.getKey()).getAssetCode());`. First collect result in a variable then check for null. `Asset asset = findAsset(assetCompositionList, pair.getKey()); if (asset != null) { assetMap.put(pair.getKey(), asset.getAssetCode()); }` Since you are calling method twice, on first call null check is there, but on second call, there is no null check. Sonar is not intelligent enough to check same parameter is passed in both call. Question is marked duplicate so can't add answer now. – Hemant Patel Mar 30 '19 at 20:40
  • 1
    @ Pratapi Hemant Patel yes just did that and came here to check if there is anyother solution. you suggested the same thank u :) – steve Mar 30 '19 at 20:48
  • 1
    @PratapiHemantPatel I agree that this was not duplicate of our canonical NPE question. Question is reopened so you can post your comment as an answer. – Pshemo Mar 31 '19 at 12:03

1 Answers1

2

Error is at line on second call of findAsset function.

assetMap.put(pair.getKey(), findAsset(assets, pair.getKey()).getAssetCode());

First collect result in a variable then check for null.

Asset asset = findAsset(assetCompositionList, pair.getKey());
if (asset != null) { 
    assetMap.put(pair.getKey(), asset.getAssetCode());
}

Since you are calling method twice, on first call null check is there, but on second call, there is no null check. Sonar is not intelligent enough to check same parameters are passed in multiple function call.

Hemant Patel
  • 3,160
  • 1
  • 20
  • 29