org.web3j.exceptions.MessageDecodingException: Value must be in format 0x[1-9]+[0-9]* or 0x0

public BigInteger getBalance(String accountId){
    try {
        DefaultBlockParameter defaultBlockParameter = new DefaultBlockParameterNumber(1);
        EthGetBalance ethGetBalance =  web3j.ethGetBalance(accountId,defaultBlockParameter).send();
        if(ethGetBalance!=null){
            return ethGetBalance.getBalance();
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return null;
}

在使用上面的代码获取账号余额的时候报错

org.web3j.exceptions.MessageDecodingException: Value must be in format 0x[1-9]+[0-9]* or 0x0

这是因为DefaultBlockParameterNumber这个类参数的值大于了当前的块数,所以报错啦。

要填写当前的块数才可以哦。

改为下面这样就好了

DefaultBlockParameter defaultBlockParameter = new DefaultBlockParameterNumber(web3j.ethBlockNumber().send().getBlockNumber());

猜你喜欢

转载自blog.csdn.net/wahaha13168/article/details/81067686