简体   繁体   English

尝试访问 Hardhat 中的 Solidity 结构数组时出现无效操作码错误

[英]Error invalid opcode trying to access Solidity array of structs in Hardhat

I have the following struct and array in my Solidity code:我的 Solidity 代码中有以下结构和数组:

    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.正如问题评论中所指出的,调用characters(0)函数时, characters数组为空。

Solidity autogenerates getter functions for public arrays, allowing to access an item of the array using its index. Solidity 为public数组自动生成 getter 函数,允许使用其索引访问数组的项目。

When you try to access an index that is out of bounds, the EVM throws an exception.当您尝试访问越界的索引时,EVM 会引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用简单的Solidity合同和脚本生成无效的操作码错误 - invalid opcode error with a simple Solidity contract and script 错误:无效的 Chai 属性:安全帽上的 revertedWith - Error: Invalid Chai property: revertedWith on Hardhat 第 7 课:yarn hardhat deploy --tags mocks -> 返回错误(完整的区块链 Solidity 课程 2022 - 时间戳:10:49.24) - Lesson 7: yarn hardhat deploy --tags mocks -> returns error (Full Blockchain Solidity Course 2022 - timestamp: 10:49.24) Solidity 错误““未定义”的参数数量无效” - Solidity error 'Invalid number of parameters for "undefined" ' 如何使用安全帽和以太币监控 Solidity 智能合约上的事件? - How to monitor events on an Solidity Smart Contract using hardhat and ethers? 在我的安全帽配置文件中声明多个 solidity 版本时遇到问题 - Facing issues with declaring multiple solidity versions in my hardhat config file 松露测试因“无效的操作码”而失败 - Truffle test failed with "invalid opcode" 尝试访问gmail api时出现无效的范围错误 - Invalid scope error when trying to access gmail api Solidity 与 Rinkeby 测试网络 | 未捕获的错误:无效地址 - Solidity w/ Rinkeby Test Network | Uncaught Error: invalid address 无效的参数个数 - Invalid Number of arguments in Solidity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM