1

I'm trying to deploy a very simple contract on Windows 10, using Web3j 4.2.0. I've created an account using Mist. I've set up one local ethereum node (using geth) on my machine and I'm able to deploy and interact with contracts using eg. Mist. But when I'm trying to do the same from Java code all the time I'm getting

org.web3j.tx.exceptions.ContractCallException: Empty value (0x) returned from contract

at org.web3j.tx.Contract.executeCallSingleValueReturn(Contract.java:250)
at org.web3j.tx.Contract.lambda$executeRemoteCallSingleValueReturn$1(Contract.java:317)
at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30)

Here is my solidity contract:

pragma solidity ^0.5.5;

contract SampleContract{
uint256 sampleVariable = 0;

    function setSample(uint256 x) public{
        sampleVariable = x;
    }
    function getSample() public view returns(uint256){
        return sampleVariable;
    }
}

I created a java wrapper class using solidity compiler and web3j CLI 4.2.0:

/**
 * <p>Auto generated code.
 * <p><strong>Do not modify!</strong>
 * <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
 * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the 
 * <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
 *
 * <p>Generated with web3j version 4.2.0.
 */
public class SampleContract extends Contract {
    private static final String BINARY = "60806040526000805534801561001457600080fd5b5060a2806100236000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806331c8039b146037578063e221818b14604f575b600080fd5b603d606b565b60408051918252519081900360200190f35b606960048036036020811015606357600080fd5b50356071565b005b60005490565b60005556fea165627a7a72305820de9e3ced16dc30cb74a4215c04b4b6f5f61df43a4bc0c38fbbd23e798b6b805d0029";

    public static final String FUNC_GETSAMPLE = "getSample";

    public static final String FUNC_SETSAMPLE = "setSample";

    @Deprecated
    protected SampleContract(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
        super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
    }

    protected SampleContract(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
        super(BINARY, contractAddress, web3j, credentials, contractGasProvider);
    }

    @Deprecated
    protected SampleContract(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
        super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
    }

    protected SampleContract(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
        super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider);
    }

    public RemoteCall<BigInteger> getSample() {
        final Function function = new Function(FUNC_GETSAMPLE, 
                Arrays.<Type>asList(), 
                Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
        return executeRemoteCallSingleValueReturn(function, BigInteger.class);
    }

    public RemoteCall<TransactionReceipt> setSample(BigInteger x) {
        final Function function = new Function(
                FUNC_SETSAMPLE, 
                Arrays.<Type>asList(new Uint256(x)),
                Collections.<TypeReference<?>>emptyList());
        return executeRemoteCallTransaction(function);
    }

    @Deprecated
    public static SampleContract load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
        return new SampleContract(contractAddress, web3j, credentials, gasPrice, gasLimit);
    }

    @Deprecated
    public static SampleContract load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
        return new SampleContract(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
    }

    public static SampleContract load(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
        return new SampleContract(contractAddress, web3j, credentials, contractGasProvider);
    }

    public static SampleContract load(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
        return new SampleContract(contractAddress, web3j, transactionManager, contractGasProvider);
    }

    public static RemoteCall<SampleContract> deploy(Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
        return deployRemoteCall(SampleContract.class, web3j, credentials, contractGasProvider, BINARY, "");
    }

    @Deprecated
    public static RemoteCall<SampleContract> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
        return deployRemoteCall(SampleContract.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
    }

    public static RemoteCall<SampleContract> deploy(Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
        return deployRemoteCall(SampleContract.class, web3j, transactionManager, contractGasProvider, BINARY, "");
    }

    @Deprecated
    public static RemoteCall<SampleContract> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
        return deployRemoteCall(SampleContract.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
    }
} 

Here is the java code of contract creation:

Credentials credentials = WalletUtils.loadCredentials(password,
        keystorePath);

SampleContract sampleContract= SampleContract.deploy(web3j, credentials, new DefaultGasProvider()).send();

LOGGER.info("Contract deployed under address: " + sampleContract.getContractAddress());

LOGGER.info("Is contract valid : " + sampleContract.isValid());

BigInteger sampleNumber = sampleContract.getSample().send();

I'm able to deploy contract, get its address, also check is this contract valid returns true. But when I try to read from it (the last line of code) it always throws org.web3j.tx.exceptions.ContractCallException. Do you have any idea what's the reason of this? Thanks for help.

bumped to the homepage by Community 5 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

0

use the load(...) method from SampleContract Wrapper and then call getSample()

I can't test it now but it should look something like this

// your code for deploy and wait until it deployed
SampleContract myContract = SampleContract.load(<your-contract-address>, web3j, credentails, ......etc);
BigInteger sNumber = myContract.getSample().send();   
//or BigInteger sNumber = myContract.getSample().sendAsync().get(); 
  • Unfortunately this doesn't work. The result is the same.. – GypsumLady Apr 15 at 15:26
  • same Exception?, could you try your code with Ganache instead of geth? – Majd TL Apr 15 at 17:35
  • yes, my code is working with Ganache some default workspace. Thanks! Do you know what could be the problem with geth? – GypsumLady Apr 15 at 18:57
  • 1
    ok I found the solution after your tip, this post helped also in my case (problem with wrong version of solc compiler). – GypsumLady Apr 15 at 21:49

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.