处理返回CompletionStage<Void>方法时遇NullPointerException求助
初始错误信息
java.util.concurrent.CompletionException: java.lang.NullPointerException: Cannot invoke "java.util.concurrent.CompletionStage.toCompletableFuture()" because the return value of "java.util.function.Function.apply(Object)" is null
错误定位到这段代码的thenCompose(x -> x)行:
.handle( (result, ex) -> { if (ex == null) { return null; } else if (ex.getCause() instanceof NotFoundException) { return configStore.doSomething(); /** 返回CompletionStage<Void> **/ } throw new CompletionException(ex.getCause()); }) .thenCompose(x -> x); /** 错误指向此处 **/
错误原因
handle方法不会自动解包CompletionStage,当内部返回configStore.doSomething()(类型为CompletionStage<Void>)时,外层会变成CompletionStage<CompletionStage<Void>>,因此需要thenCompose来解包嵌套的CompletionStage。但当ex == null时直接返回null,thenCompose尝试调用null对象的toCompletableFuture()方法,触发空指针异常。
尝试修复后的新问题
将return null改为return CompletableFuture.completedFuture(null)后,出现编译错误:
不兼容的类型: 推断类型不符合上限
推断: java.util.concurrent.CompletableFuture
上限: java.util.concurrent.CompletionStage<java.lang.Void>,java.lang.Void
正确解决方案
核心问题是handle方法的返回值类型不统一,导致泛型推断混乱。需要让handle的所有分支都返回非null的CompletionStage<Void>类型,并可通过显式指定泛型解决推断问题:
方案1:统一返回类型并显式指定泛型
.<CompletionStage<Void>>handle( (result, ex) -> { if (ex == null) { // 返回已完成的Void类型CompletionStage,替代null return CompletableFuture.completedFuture(null); } else if (ex.getCause() instanceof NotFoundException) { return configStore.doSomething(); } throw new CompletionException(ex.getCause()); }) .thenCompose(x -> x);
方案2:用allOf()返回空的已完成阶段(更语义化)
如果ex == null时不需要执行任何操作,用CompletableFuture.allOf()返回一个已完成的CompletionStage<Void>,语义更清晰:
.<CompletionStage<Void>>handle( (result, ex) -> { if (ex == null) { return CompletableFuture.allOf(); } else if (ex.getCause() instanceof NotFoundException) { return configStore.doSomething(); } throw new CompletionException(ex.getCause()); }) .thenCompose(x -> x);
这样既解决了初始的空指针问题,也修复了后续的编译错误,同时保留了thenCompose解包嵌套CompletionStage的作用。
内容的提问来源于stack exchange,提问作者Andrew Cheong

