java.lang.NullPointerException 在 org.web3j.protocol.core.filters.LogFilter.process(LogFilter.java:46)

java.lang.NullPointerException at org.web3j.protocol.core.filters.LogFilter.process(LogFilter.java:46)

我正在尝试使用 web3j 读取区块链事件(在 Java 中)但得到 NPE:

java.lang.NullPointerException: null
    at org.web3j.protocol.core.filters.LogFilter.process(LogFilter.java:46)
    at org.web3j.protocol.core.filters.Filter.getInitialFilterLogs(Filter.java:119)
    at org.web3j.protocol.core.filters.Filter.run(Filter.java:69)
    at org.web3j.protocol.rx.JsonRpc2_0Rx.run(JsonRpc2_0Rx.java:89)
    at org.web3j.protocol.rx.JsonRpc2_0Rx.lambda$ethLogFlowable(JsonRpc2_0Rx.java:79)
    at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:71)
    at io.reactivex.Flowable.subscribe(Flowable.java:14935)
    at io.reactivex.Flowable.subscribe(Flowable.java:14872)
    at io.reactivex.Flowable.subscribe(Flowable.java:14791)

有问题的代码

for (EthLog.LogResult logResult : logResults) { https://github.com/web3j/web3j/blob/master/core/src/main/java/org/web3j/protocol/core/filters/LogFilter.java#L46

    @Override
    protected void process(List<EthLog.LogResult> logResults) {
        for (EthLog.LogResult logResult : logResults) {
            if (logResult instanceof EthLog.LogObject) {
                Log log = ((EthLog.LogObject) logResult).get();
                callback.onEvent(log);
            } else {
                throw new FilterException(
                        "Unexpected result type: " + logResult.get() + " required LogObject");
            }
        }
    }

提出问题https://github.com/web3j/web3j/issues/1486 没想到修好了,怎么办?

    private void createEventMonitor() {
        log.info("createEventMonitor() begin...");
        // contract from block
        EthFilter filter = new EthFilter(DefaultBlockParameter.valueOf(new BigInteger("7605105")),
                DefaultBlockParameterName.LATEST, contractAddress);

        //Disposable subscription
        subscription = web3j
                .ethLogFlowable(filter)
                .subscribe(
                        event -> {
            log.info("Withdraw event received:");
            log.info("  event data >>> {}", event.getData());
            log.info("  event topic >>> {}", event.getTopics().stream().collect(Collectors.joining()));
            log.info("  event address >>> {}", event.getAddress());
            log.info("  event txHash >>> {}", event.getTransactionHash());
        }, error -> {
            log.error("Event error: {}", error, error); //!
        });

        log.info("createEventMonitor() end.");
    }

这个错误应该会在 https://github.com/web3j/web3j/pull/1495 solving https://github.com/web3j/web3j/issues/1486 之后消失(应该是 4.8.7 之后的下一个版本)

简而言之:JSON RPC 错误在 web3j 级别上不可见。

要分析这样的问题 运行 日志级别为 debug 的程序以查看 JSON RPC 消息。