I have the following struct and array in my Solidity code:
struct Character {
int256 strength;
uint256 dexterity;
uint256 constitution;
....
}
Character[] public characters;
I have the following line in my Hardhat test attempting to access a member of that array:
const character = await contract.characters(0)
I then get the following error:
Error: VM Exception while processing transaction: invalid opcode
at Contract.characters
What is the correct way to access a member of this array of structs?
As pointed out in the question comment, the characters
array was empty when the characters(0)
function was called.
Solidity autogenerates getter functions for public
arrays, allowing to access an item of the array using its index.
When you try to access an index that is out of bounds, the EVM throws an exception.