web3j icon indicating copy to clipboard operation
web3j copied to clipboard

GetBlocks cause NullPointerException

Open mggger opened this issue 3 years ago • 2 comments

GetBlocks cause NullPointerException

Web3j web3j =
        Web3j.build(
            new HttpService(web3jUri));
    
    Iterable<EthBlock> eths = web3j.replayPastBlocksFlowable(
            new DefaultBlockParameterNumber(startBlock),
            new DefaultBlockParameterNumber(endBlock), true)
            .blockingIterable();


     eths.forEach(eth -> {
      String printStr = eth.getBlock().toString();

      System.out.println(printStr);
    });

it will cause

org.web3j.protocol.core.methods.response.EthBlock$Block@9a316cc5
org.web3j.protocol.core.methods.response.EthBlock$Block@c14e6975
org.web3j.protocol.core.methods.response.EthBlock$Block@c20ce720
org.web3j.protocol.core.methods.response.EthBlock$Block@4cee7328
org.web3j.protocol.core.methods.response.EthBlock$Block@1ae94be0
Exception in thread "main" java.lang.NullPointerException
	at Web3jMain.lambda$main$0(Web3jMain.java:65)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at Web3jMain.main(Web3jMain.java:64)

Steps To Reproduce

  1. Set up a local eth node by this
  2. start miner

Expected behavior

No errors throw

Actual behavior

Throw nullPointerException

Environment

Web3j version: 5.0.0

Additional context

null

mggger avatar Apr 24 '22 09:04 mggger

org.web3j.protocol.Service #send

the size of result buffer is zero

img

mggger avatar Apr 24 '22 09:04 mggger

you iterate EthBlock from startBlock -> endBlock , but actually there is not enough block in your local Ethereum. So, eths.forEach will get null at runtime. image

Replux avatar Apr 24 '22 11:04 Replux