CompletableFuture.allof() 获取空指针异常

问题描述 投票:0回答:1

我的结构类似于下面的代码块。不知何故,我在 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()]));

java asynchronous java-8 nullpointerexception completable-future
1个回答
0
投票

查看CompletableFutre.andTree

源代码,似乎当传入数组的元素为
null
时抛出此异常。在您的
return
函数之一中必须有一些额外的
asyncProcees
语句可以返回
null

© www.soinside.com 2019 - 2024. All rights reserved.