3

I have a completable future code which throws NPE. I am unable to reproduce the exception in my local.

I have tried acceptance test and unit test for the code but it's not giving the exact error response.

    final List<CompletableFuture<Map<K, V>>> completableFutures = 
    copy.stream()                                                                         
    .map(copy -> getRespForId(requestContext, copy))                                                                          
    .collect(Collectors.toList());

    return CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture<?>[0]))
               .thenApply(future -> completableFutures.stream()
                                        .map(CompletableFuture::join)
                                        .flatMap(longMap -> longMap.entrySet().stream())
                                        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));

Expected is

Caused by: java.util.concurrent.CompletionException: java.lang.NullPointerException
    at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(Unknown Source)
    at java.base/java.util.concurrent.CompletableFuture.completeThrowable(Unknown Source)
    at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Source)
    at java.base/java.util.concurrent.CompletableFuture.postComplete(Unknown Source)
    at java.base/java.util.concurrent.CompletableFuture.postFire(Unknown Source)
    at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Source)
    at java.base/java.util.concurrent.CompletableFuture$Completion.exec(Unknown Source)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Unknown Source)
    at java.base/java.util.concurrent.ForkJoinPool.scan(Unknown Source)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
Caused by: java.lang.NullPointerException: null
    at java.base/java.util.Objects.requireNonNull(Unknown Source)
    at java.base/java.util.stream.Collectors.lambda$uniqKeysMapAccumulator$1(Unknown Source)
Didier L
  • 18,905
  • 10
  • 61
  • 103
Pooja Mahapatra
  • 105
  • 3
  • 11
  • Based on the class name `Collectors.uniqKeysMapAccumulator` I would assume that the `Collectors.toMap` step is the problem source. For one element the key seems to be `null`. Therefore in one of your `longMap`s there is a null value. – Robert Apr 15 '19 at 18:15
  • I don't think the key can be null since it's coming from the request and the request is validated. – Pooja Mahapatra Apr 16 '19 at 03:51
  • 1
    OK, then theoretically you don't have a NullPointerException... – Robert Apr 16 '19 at 07:20
  • Does this answer your question? [NullPointerException in Collectors.toMap with null entry values](https://stackoverflow.com/questions/24630963/nullpointerexception-in-collectors-tomap-with-null-entry-values) – Sebastian Jun 22 '21 at 06:54

1 Answers1

0

The problem here is not the keys but the values. You get this exception because a value is null. Looks like java.util.stream.Collectors#uniqKeysMapAccumulator checks Objects.requireNonNull on the values. See NullPointerException in Collectors.toMap with null entry values

Sebastian
  • 5,721
  • 3
  • 43
  • 69