【问题标题】:Solidity Error Message on Deployment: "Cannot Convert Undefined or Null to Object"部署时的 Solidity 错误消息:“无法将未定义或空值转换为对象”
【发布时间】:2021-08-13 01:57:16
【问题描述】:

我正在学习 udemy 课程,但在我的代码中遇到错误:

创建彩票出错:无法将 undefined 或 null 转换为对象

为了测试这是否是我的错误,我使用了讲师提供的源代码,但是出现了同样的问题。我可以看到这段代码最近对其他学生有效。有什么建议吗?

以下是教程的源代码:

//SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.5.0

合约彩票{

// declaring the state variables
address payable[] public players; //dynamic array of type address payable
address public manager; 


// declaring the constructor
constructor(){
    // initializing the owner to the address that deploys the contract
    manager = msg.sender; 
}

// declaring the receive() function that is necessary to receive ETH
receive () payable external{
    // each player sends exactly 0.1 ETH 
    require(msg.value == 0.1 ether);
    // appending the player to the players array
    players.push(payable(msg.sender));
}

// returning the contract's balance in wei
function getBalance() public view returns(uint){
    // only the manager is allowed to call it
    require(msg.sender == manager);
    return address(this).balance;
}

// helper function that returns a big random integer
function random() internal view returns(uint){
   return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players.length)));
}


// selecting the winner
function pickWinner() public{
    // only the manager can pick a winner if there are at least 3 players in the lottery
    require(msg.sender == manager);
    require (players.length >= 3);
    
    uint r = random();
    address payable winner;
    
    // computing a random index of the array
    uint index = r % players.length;

    winner = players[index]; // this is the winner
    
    // transferring the entire contract's balance to the winner
    winner.transfer(getBalance());
    
    // resetting the lottery for the next round
    players = new address payable[](0);
}

}

【问题讨论】:

    标签: solidity


    【解决方案1】:

    只需复制并粘贴您在 remix 中提供的相同代码,即可部署合约实例。您可以尝试重新加载浏览器,例如 this thread

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      • 2022-01-16
      • 2021-01-20
      • 2020-03-21
      • 1970-01-01
      • 2020-06-09
      相关资源
      最近更新 更多