0

Gas estimation failed error while invoking a simple function

pragma solidity ^0.5.9;

contract sample{
    uint number;
    function setNumber(uint _number) public{
        number = _number;
    }
    function getNumber() view public returns(uint){
        return number;
    }
}

It's deployed successfully on localhost:8545. But while checking the function from remix it gave me error.

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? VM Exception while processing transaction: invalid opcode

while working fine on javascript VM.

  • I tried deploying in Remix's javascript VM and am able to execute both the functions in your contract. How did you load the contract on local ganache on to Remix? check if you have given the correct address. – Sanjay S B Jun 7 at 8:16
0

From solidity compiler version 0.5.5 it is compulsory to initialize contract members using constructor only so you have to initialize like this:

pragma solidity ^0.5.9;

contract sample{
    uint number;

    constructor(uint _number)public{
        number = _number;
    }


    function getNumber() view public returns(uint256){
        return number;
    }
}

If you want to go with your code then change its compiler version 0.5.9 to 0.5.4. But I recommend you to the initilize member using constructor only.

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.