简体   繁体   English

试图与web3j(Android)取得平衡,出现nullpointer异常

[英]Trying to get balance with web3j (Android), geting nullpointer exception

Struggling getting anything to work in web3j on android, trying this example (I have replaced token and addr) the address I used does have some rinkeby ETH in it. 努力使任何东西都可以在android上的web3j中工作,尝试使用本示例(我已替换了token和addr),我使用的地址确实包含一些rinkeby ETH。 Testing on phone, application crashes when I load up this class/activity, code is in the oncreate method. 在电话上测试,加载此类/活动时应用程序崩溃,代码在oncreate方法中。 Have internet permission turned on in the manifest, and compile android web3j in the build gradle. 在清单中打开互联网许可,然后在构建gradle中编译android web3j。

 Web3j web3 = Web3jFactory.build(new 
 HttpService("https://rinkeby.infura.io/token"));

    EthGetBalance ethGetBalance = null;
    try {
        ethGetBalance = web3
                .ethGetBalance("addr",DefaultBlockParameterName.LATEST)
                .sendAsync()
                .get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    BigInteger wei = ethGetBalance.getBalance();

The error 错误

06-30 02:15:47.115 18904-18904/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.test.user.test, PID: 18904
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.user.test/com.test.user.test.balance}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2607)
    at android.app.ActivityThread.access$900(ActivityThread.java:174)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5756)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at com.test.user.test.balance.onCreate(balance.java:43)
    at android.app.Activity.performCreate(Activity.java:5619)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2512)

Line 43 referenced in error is the last line of code in my submitted code. 错误引用的第43行是我提交的代码中的最后一行代码。 When I comment out that line app doesn't crash and just get a few (what I assume are all) warnings. 当我注释掉该行应用程序不会崩溃时,只会收到一些(我认为是全部)警告。

Your ethGetBalance return a null value .i think you put a invalid address. 您的ethGetBalance返回一个空值。我认为您输入了无效的地址。 thats why method web3j.ethGetBalance failes. 那就是为什么方法web3j.ethGetBalance失败的原因。 try this with valid address. 尝试使用有效地址。

    Web3j web3 = Web3jFactory.build(new HttpService("https://ropsten.infura.io/your-token"));

    String strAddress="0x24b********************"; //strAddress is your wallet address
    EthGetBalance ethGetBalance=
    web3.ethGetBalance(strAddress,DefaultBlockParameterName.LATEST).sendAsync().get();

    BigInteger wei = ethGetBalance.getBalance();

ethGetBalance.getBalance() is used to get your ether balance in wei. ethGetBalance.getBalance()用于获取wei中的以太币余额。 To get actual token in your wallet use this Conversion method. 要在您的钱包中获取实际令牌,请使用此Conversion方法。

  java.math.BigDecimal tokenValue = Convert.fromWei(String.valueOf(wei), Convert.Unit.ETHER);
  String strTokenAmount = String.valueOf(tokenValue);

ethGetBalance = web3.ethGetBalance(public_address, DefaultBlockParameterName.LATEST).sendAsync().get(); ethGetBalance = web3.ethGetBalance(public_address,DefaultBlockParameterName.LATEST).sendAsync()。get(); BigInteger wei = ethGetBalance.getBalance(); BigInteger wei = ethGetBalance.getBalance(); Log.e("ether balance", "wei" + wei); Log.e(“ ether balance”,“ wei” + wei); This is working fine in my app which is available on playstore 我的应用可以在Playstore上正常运行

https://play.google.com/store/apps/details?id=multi.erc20.devgenesis.token.wallet https://play.google.com/store/apps/details?id=multi.erc20.devgenesis.token.wallet

The problem is that the ethGetBalance is null, when you want to access it, because the method web3j.ethGetBalance fails. 问题在于,当您要访问ethGetBalance时,该方法为null,因为方法web3j.ethGetBalance失败。

You are sending "addr" as an address, this is not a valid Ethereum address. 您正在发送“ addr”作为地址,这不是有效的以太坊地址。

A valid Ethereum address has a format similar that begins with: 0x and then has 40 elements (digits 0-9 or letters A to F). 有效的以太坊地址的格式类似于以0x开头,然后包含40个元素(数字0-9或字母A至F)。 To see if your address exists, just try to find it in the website https://etherscan.io/ 要查看您的地址是否存在,只需尝试在网站https://etherscan.io/中找到它即可。

Try with a correct Ethereum address and it should work. 尝试使用正确的以太坊地址,它应该可以工作。

EthGetBalance ethGetBalance = web3j
                    .ethGetBalance(userAddress, DefaultBlockParameterName.LATEST)
                    .sendAsync()
                    .get();


BigInteger wei = ethGetBalance.getBalance();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM