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. 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 = 

    EthGetBalance ethGetBalance = null;
    try {
        ethGetBalance = web3
    } catch (InterruptedException e) {
    } catch (ExecutionException e) {

    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.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at com.test.user.test.balance.onCreate(

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.

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 = HttpService(""));

    String strAddress="0x24b********************"; //strAddress is your wallet address
    EthGetBalance ethGetBalance=

    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);

java, 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​  Create the accounts before hand, for example in myetherwallet or with web3j using: web3j wallet create 2. Extract the private key or the password and walletfile. 3.

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

go ethereum, I was able to fix this issue by updating web3j version from 2.3.0 to 2.3.1 . From this link : I was able to  web3j is a lightweight, highly modular, reactive, type safe Java and Android library for working with Smart Contracts and integrating with clients (nodes) on the Ethereum network: This allows you to work with the Ethereum blockchain, without the additional overhead of having to write your own integration code for the platform.

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

Try with a correct Ethereum address and it should work.

EthGetBalance ethGetBalance = web3j
                    .ethGetBalance(userAddress, DefaultBlockParameterName.LATEST)

BigInteger wei = ethGetBalance.getBalance();

org.web3j.protocol.Web3j.ethGetBalance java code examples , public BigInteger getBalance(String fromAddress){ BigInteger balance = null; try { EthGetBalance ethGetBalance = web3j.ethGetBalance(fromAddress  I Bought a $3 2TB USB Drive and Got More Than Just Malware - Duration: 11:18. Jays Tech Vault Recommended for you

web3j/web3j, Usin the latest web3j library I got an exception, java.lang. I am using web3 1.0 beta 41 and in my node js code I am trying to listen to my log can be found here​: Check comment where I get null pointer. web3j, credentials, null); //getting Null pointer at SEND. Getting NullPointer exception with File.listfiles() java,android,nullpointerexception. So I have 3 folders in my Android Gallery. I am trying to read the files of

Manage an Ethereum account with Java and Web3j, Hi all, I try to tigger multiple contact create transactions at the same time but only one will make it to the chain. The others fail with a "Transaction Reciept Timeout  This article explains about transfer ether from one account to another account. We need to do following steps for ether transfer- You need to add web3j dependency in maven as

Java NullPointerException, Contract accounts, which are controlled by their contract code and have code Using our Web3j instance (see article-1), it is possible to retrieve the balance of an String password = null; // no encryption String mnemonic = "candy maple cake At this point, the client can claim a transaction receipt by transaction hash to  When using the web3j lib for blockchain transaction in my private test blockchain I'm currently running into the titled response error: *insufficient funds for gas * price + value* The account from which I want to transfer some ether has a balance of 10000 ether.

  • 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.
  • Changed to .send(); with catch (IOException ex) { ex.printStackTrace(); } but still same error.
  • 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.
  • 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.
  • 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.
  • nice one, gaurav
  • 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.