Search code examples
javaeventsnullpointerexceptionsolidityweb3-java

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


I am trying to read blockchain event (in Java) using web3j but getting 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$2(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)

Code in question

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");
            }
        }
    }

Raised issue https://github.com/web3j/web3j/issues/1486 But as not expected that to be fixed, what should I do?

  • Sample code and/or code snippets
    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.");
    }

Solution

  • This error should go away in the future after https://github.com/web3j/web3j/pull/1495 solving https://github.com/web3j/web3j/issues/1486 (should be next version after 4.8.7)

    In short: JSON RPC error was no visible on web3j level.

    To analyze issue like that run program with log level debug to see JSON RPC messages.