gpt4 book ai didi

java - 线程 "main"java.lang.NullPointerException 位于reactor.core.publisher.FluxFilter$FilterSubscriber.onNext 中出现异常

转载 作者:行者123 更新时间:2023-12-01 19:58:49 25 4
gpt4 key购买 nike

在过滤方法之后,订阅方法似乎给出了一个空指针异常。当我删除过滤方法时,一切正常。为什么?

        Flux<String> flux = new Flux<String>() {
@Override
public void subscribe(CoreSubscriber<? super String> coreSubscriber) {
coreSubscriber.onNext("Test 7");
coreSubscriber.onNext("test 6"); //line 33
coreSubscriber.onNext("test 5");
coreSubscriber.onNext("test 4");
coreSubscriber.onNext("test 3");
coreSubscriber.onNext("test 2");
coreSubscriber.onNext("test 1");
coreSubscriber.onComplete();
}
};

flux
.filter((s) -> s.contains("7"))
.subscribe(System.out::println); //line 45

我收到以下错误:

Exception in thread "main" java.lang.NullPointerException
at reactor.core.publisher.FluxFilter$FilterSubscriber.onNext(FluxFilter.java:100)
at com.douma.reactor.App$1.subscribe(App.java:33)
at reactor.core.publisher.FluxFilter.subscribe(FluxFilter.java:52)
at reactor.core.publisher.Flux.subscribe(Flux.java:6873)
at reactor.core.publisher.Flux.subscribeWith(Flux.java:7040)
at reactor.core.publisher.Flux.subscribe(Flux.java:6866)
at reactor.core.publisher.Flux.subscribe(Flux.java:6830)
at reactor.core.publisher.Flux.subscribe(Flux.java:6773)
at com.douma.reactor.App.test2(App.java:45)
at com.douma.reactor.App.main(App.java:24)

最佳答案

您正在 subscribe 方法内设置 Flux 容器的初始值。当你调用filter时,初始值仍然是null,因此contains会抛出异常。将其添加到您的代码中:

    flux.startWith("starter string")
.filter((s) -> s.contains("7") || s.contains("starter"))
.subscribe(System.out::println); // Prints out "starter string" and "Test 7"

关于java - 线程 "main"java.lang.NullPointerException 位于reactor.core.publisher.FluxFilter$FilterSubscriber.onNext 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59018535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com