2

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. Testing on phone, application crashes when I load up this class/activity, code is in the oncreate method. Have internet permission turned on in the manifest, and compile android web3j in the build gradle.

 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. When I comment out that line app doesn't crash and just get a few (what I assume are all) warnings.

  • I ran your snippet in my IDE and it worked fine. Have you tried using the synchronous version? Try replacing `.sendAsync().get()` with `.send()`. Since you're calling a view function, you don't need to use the asynchronous call. – Adam Kipnis Jul 01 '18 at 20:11
  • Changed to .send(); with catch (IOException ex) { ex.printStackTrace(); } but still same error. – web derpveloper Jul 02 '18 at 01:18
  • I believe you have something else going on outside the scope of what is posted. The code works even when you have an invalid token or wrong Ethereum address. It also worked for me using both sync and async approaches. You would have to be getting another stacktrace from one of your 2 catch statements which is then failing with an NPE (since all you're doing is printing the stacktrace and not handling the error). Break your code down and run it through a debugger. That's the best advice I can give. – Adam Kipnis Jul 02 '18 at 16:18
  • It says variable wei is never used, in android studio.. I don't know if I'm missing something here, I'm not great at java so not sure which steps to take to get web3j working. – web derpveloper Jul 03 '18 at 01:12
  • Think my issue was an ssl handshake failiure in a low version of android (4.4.4) thus not returning anything, will try and work through that problem. But it works in emulator using api25. – web derpveloper Jul 07 '18 at 02:43

3 Answers3

6

Your ethGetBalance return a null value .i think you put a invalid address. thats why method web3j.ethGetBalance failes. 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. To get actual token in your wallet use this Conversion method.

  java.math.BigDecimal tokenValue = Convert.fromWei(String.valueOf(wei), Convert.Unit.ETHER);
  String strTokenAmount = String.valueOf(tokenValue);
Guarav
  • 73
  • 1
  • 8
0

ethGetBalance = web3.ethGetBalance(public_address, DefaultBlockParameterName.LATEST).sendAsync().get(); BigInteger wei = ethGetBalance.getBalance(); Log.e("ether balance", "wei" + wei); This is working fine in my app which is available on playstore

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

0

The problem is that the ethGetBalance is null, when you want to access it, because the method web3j.ethGetBalance fails.

You are sending "addr" as an address, this is not a valid Ethereum address.

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). To see if your address exists, just try to find it in the website 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();
Alex Bean
  • 493
  • 7
  • 19
  • 1
    In my first sentence I mention I replaced address and token. The problem is the okhttp connection used in web3j doesnt work on pre 5.0 android devices due to SSL handshake error, caused by TLS1.2 off by default. – web derpveloper Aug 07 '18 at 16:23