I am trying to make a transaction between two different accounts using Java. It works perfectly fine as long as I am not using "Data" parameter.

My intent is not to call a smart contract but to make a value exchange between two parties where I will store small ascii (hex converted) data as a note in every transaction.

If I do not use "Data" parameter api like


then it works fine and successfully returns me the transaction hash.

But I would use API like


Then I face a JSON error like

java.util.concurrent.ExecutionException: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: buffer(okhttp3.internal.http1.Http1Codec$ChunkedSource@7905a0b8).inputStream(); line: 1, column: 81] (through reference chain: org.web3j.protocol.core.methods.response.EthSendTransaction["error"]->org.web3j.protocol.core.Response$Error["data"])

Either I am calling in a wrong way or API has any issue , I would like someone to help to understand how I can make a value exchange transaction without this above problem.

Here is my code

//create raw transaction

            //import the address from Ganache's account by using its private key
            String strPrivateKey = "0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3";
            Credentials senderCredentials = Credentials.create(ECKeyPair.create(hexStringToByteArray(strPrivateKey)));

            BigInteger amountWei = Convert.toWei("1.789", Convert.Unit.ETHER).toBigInteger();

//Data I am trying to send with transaction to be recorded in DATA part of ethereum transaction
            String Data = Numeric.toHexString("Hey this is me".getBytes());

            EthGetTransactionCount ethGetTransactionCount = web3a.ethGetTransactionCount(
                    senderCredentials.getAddress(), DefaultBlockParameterName.LATEST).sendAsync().get();

            BigInteger nonce = ethGetTransactionCount.getTransactionCount();

            //When I create transaction using below API then it crash at the time of send ethSendRawTransaction
            RawTransaction rawTransaction = RawTransaction.createTransaction(
                    nonce,Transfer.GAS_PRICE,Transfer.GAS_LIMIT, RecieverAddress,amountWei,Data);

            // When I create transaction using this API then it sucessfully work at the time of send call ethSendRawTransaction
           /* RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
                   nonce, Transfer.GAS_PRICE, Transfer.GAS_LIMIT, RecieverAddress, amountWei);*/

            byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, senderCredentials);
            String hexValue = Numeric.toHexString(signedMessage);

//this is the place where I get error 
                EthSendTransaction ethSendTransaction =  web3a.ethSendRawTransaction(hexValue).sendAsync().get();

            String transactionHash = ethSendTransaction.getTransactionHash();


From the web3j doc :/

        <nonce>, GAS_PRICE, GAS_LIMIT, "0x<address>", <amount>, "0x<hex encoded text>");

byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
String hexValue = Numeric.toHexString(signedMessage);

EthSendTransaction ethSendTransaction =
String transactionHash = ethSendTransaction.getTransactionHash();

It was the problem using the default GAS limitation. I got this information once I tried Logback. Therefore, I have changed the gas limitation by replacing default Transfer.Gast_LIMIT to 210000L.

RawTransaction rawTransaction = RawTransaction.createTransaction(
                    nonce, Transfer.GAS_PRICE, BigInteger.valueOf(210000L), RecieverAddress, amountWei, Data);

Afterward, my data worked and stored successfully.

Note:- Make sure to open the logging mechanism to understand proper error otherwise like me you will get

java.util.concurrent.ExecutionException: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out

Which doesn't give you much detail about why JSON communication was failed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.