Search code examples
solidityremix

Solidity - Invalid BigNumber string (argument="value" value="" code=INVALID_ARGUMENT version=bignumber/5.4.2)


solidity newbie here. when I try to read the value of the people array. I'm getting an error:

call to SimpleStorage.people errored: Error encoding arguments: Error: invalid BigNumber string (argument="value" value="" code=INVALID_ARGUMENT version=bignumber/5.4.2)

my compiler version is 0.6.6. not sure what's wrong? any suggestions?

// SPD-License_Identifier: MIT

pragma solidity ^0.6.0;

contract SimpleStorage {
    uint256 favNum;
    
    struct People {
        uint256 favNum;
        string name;
    }
    
    People[] public people;
    
    function store(uint256 _favNum) public {
        favNum = _favNum;
    }
    
    function retrieve() public view returns(uint256) {
        return favNum;
    }
    
    function addPerson(string memory _name, uint256 _favNum) public {
        people.push(People(_favNum, _name));
    }
}

Solution

  • The error happens when you're trying to call the people() function (from Remix IDE) without passing any value.

    Since the People[] public people is a public property, it autogenerates a getter function during compilation. But because it's an array, the getter function requires an uint256 param specifying the index of the array that you want to retrieve.

    When you pass an empty string, Remix tries to encode it to the BigNumber instance, but this fails. Only when you pass an (existing) index of the array, it works correctly:

    people() call


    If you want to get the whole array in one call, you need to create a separate getter function:

    function getAllPeople() public view returns (People[] memory) {
        return people;
    }
    

    getAllPeople() call