簡體   English   中英

試圖與web3j(Android)取得平衡,出現nullpointer異常

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

努力使任何東西都可以在android上的web3j中工作,嘗試使用本示例(我已替換了token和addr),我使用的地址確實包含一些rinkeby ETH。 在電話上測試,加載此類/活動時應用程序崩潰,代碼在oncreate方法中。 在清單中打開互聯網許可,然后在構建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();

錯誤

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)

錯誤引用的第43行是我提交的代碼中的最后一行代碼。 當我注釋掉該行應用程序不會崩潰時,只會收到一些(我認為是全部)警告。

您的ethGetBalance返回一個空值。我認為您輸入了無效的地址。 那就是為什么方法web3j.ethGetBalance失敗的原因。 嘗試使用有效地址。

    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()用於獲取wei中的以太幣余額。 要在您的錢包中獲取實際令牌,請使用此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(); BigInteger wei = ethGetBalance.getBalance(); Log.e(“ ether balance”,“ wei” + wei); 我的應用可以在Playstore上正常運行

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

問題在於,當您要訪問ethGetBalance時,該方法為null,因為方法web3j.ethGetBalance失敗。

您正在發送“ addr”作為地址,這不是有效的以太坊地址。

有效的以太坊地址的格式類似於以0x開頭,然后包含40個元素(數字0-9或字母A至F)。 要查看您的地址是否存在,只需嘗試在網站https://etherscan.io/中找到它即可。

嘗試使用正確的以太坊地址,它應該可以工作。

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