0

I have created a contract and trying to run the contract on the Remix IDE. Following is the code of my contract:

pragma solidity 0.5.9;
contract MyContract{
   
   uint256  peopleCount;
   struct Person{
       string   _firstName;
       string   _lastName;
   }
   Person[]  public  people;
   function addPerson(string memory _firstName, string memory _lastName) public{
      
      people.push(Person(_firstName, _lastName));
      peopleCount += 1;
   }
    function  getFirstName() public view returns (string memory) {
       return people[0]._firstName;
   }
   function   getLastName() public view returns (string memory){
       return people[0]._lastName;
   }
      function setFirstName(string memory _val) public {
      people[0]._firstName = _val;
   }

}

I have attached an image when I click the setFirstName(..) tab, I get a trasact button and a box for providing the value of _val argument as shown in the following image:

setFirstName method error

[vm] from: 0x5B3...eddC4to: MyContract.setFirstName(string) 0x7EF...8CB47value: 0 weidata: 0x639...00000logs: 0hash: 0xf02...883db transact to MyContract.setFirstName errored: VM error: invalid opcode. invalid opcode The execution might have thrown. Debug the transaction to get more information.

I dont know how to do the debugging, somebody please guide me.

Zulfi.

1

To access an array item people[index] with an index it has to be a valid position, ie index >= 0 and index < people.length.

In your example people[0]._firstName = _val will fail if people is empty. If you first call addPerson it should work.

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.