我的结构类似于下面的代码块。不知何故,我在 CompletableFuture.allOf 方法中得到 nullpointerException: 这里可能有什么问题? 谢谢..
List<CompletableFuture<String>> completableFutures = new ArrayList<>();
for(Classx list : request.getlist()) {
switch (list.getCases()) {
case 1:
completableFutures.add(asyncProcess(list));
break;
case 2:
completableFutures.add(asyncProcess2(list));
break;
}
}
CompletableFuture<Void> allFutures = CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0]));
@Async
public CompletableFuture<String> asyncProcess(Classx list) {
try{
//..some codes
} catch(Exception e){
//..some codes
}
return CompletableFuture.completedFuture(x.getX());
}
@Async
public CompletableFuture<String> asyncProcess2(Classx list) {
try{
//..some codes
} catch(Exception e){
//..some codes
}
return CompletableFuture.completedFuture(x.getX());
}
错误信息是:
java.lang.NullPointerException
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1306)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1303)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1305)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1305)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1303)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1305)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1303)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1305)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1305)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1303)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1303)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1303)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1303)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1305)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1303)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1303)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1305)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1303)
at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1303)
at java.util.concurrent.CompletableFuture.allOf(CompletableFuture.java:2225)
当我按如下方式更改代码时,它会引发相同的错误。
CompletableFuture allFutures = CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[completableFuture.size()]));