Remix IDE: Problem with Executing 'set' method

Hi,
I am trying to execute the following contract on the Remix IDE. When I deploy the contract, Remix IDE shows me the tabs of all the functions. When I click the setFirstName(…) function, it asks the input for the variable _val, when I give the value and press the trasact button, I get the following 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.

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

}

The image of remix IDE is:

I don’t know how to do the debugging. Somebody please guide me.

Zulfi.

I think you should call addPerson() at first, and then you can call setFirstName()

2 Likes