我有一个完整的将来代码,该代码将引发NPE。我无法在本地复制该异常。
我已经为代码尝试了验收测试和单元测试,但是没有给出确切的错误响应。
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)));
预期是
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)
答案 0 :(得分:0)
这里的问题不是键而是值。您会收到此异常,因为值为 null。看起来 java.util.stream.Collectors#uniqKeysMapAccumulator
检查 Objects.requireNonNull
的值。见NullPointerException in Collectors.toMap with null entry values