Search code examples
javaethereumgo-ethereum

Cannot make a static reference to the non-static method ethGetBlockByNumber(DefaultBlockParameter, boolean) from the type Ethereum


I am not able to understand what is wrong with my code.

public void GetLatestBlock() throws Exception{
    List<EthBlock.TransactionResult> txs = Web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, 
    true).send().getBlock().getTransactions();
    //Ttxs =TestGet();
    
    txs.forEach(tx -> {
      EthBlock.TransactionObject transaction = (EthBlock.TransactionObject) tx.get();

      System.out.println(transaction.getFrom());
    });

It shows error : Cannot make a static reference to the non-static method ethGetBlockByNumber(DefaultBlockParameter, boolean) from the type Ethereum

I want to get lastblock's receipt for calculate the hashrate.

please give me some advice. Thanks a lot.


Solution

  • Web3j.ethGetBlockByNumber is not static (there is no static modification in signature of ethGetBlockByNumber). You must replace class name Web3j by instance (object of type Web3j) Web3j.

    For example:

    Web3j web3j = new Web3j(); // Or another way to create object of type Web3j
    web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, 
        true).send().getBlock().getTransactions();