3

I just begun to learn solidity by following a youtube video(https://www.youtube.com/watch?v=M576WGiDBdQ). This is duplicated question for Why do I keep getting invalid argument?, but I can't resolve the problem with the answer on the article. And I have not enough reputation for post a comment.

Entire code is as below.(and it works fine on the video)

pragma solidity ^0.6.0;

contract SimpleStorage {
    
    // This will get initialized to 0!
    uint256 favoriteNumber;
    bool favoriteBool;

    struct People {
        uint256 favoriteNumber;
        string name;
    }
    
    People[] public people;
    mapping(string => uint256) public nameToFavoriteNumber;

    function store(uint256 _favoriteNumber) public {
        favoriteNumber = _favoriteNumber;
    }

    // view, pure
    function retrieve() public view returns(uint256) {
        favoriteNumber + favoriteNumber;
    }

    function addPerson(string memory _name, uint256 _favoriteNumber) public{    
        people.push(People(_favoriteNumber, _name));
        nameToFavoriteNumber[_name] = _favoriteNumber;
    }
    
}

Other features are working with no errors but the accessing 'people' keeps give me a error

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

kassé suggested on the article(Why do I keep getting invalid argument?)

when you deploy put 0 on the people variable and click on it after you can add with de addPerson function. Also if you want to add second person put 1 to people and after add another one

I tried modify code as below:

People[0] public people;

and

People[1] public people;

It gives me below message

contracts/SimpleStorage.sol:12:12: TypeError: Array with zero length specified.

and

contracts/SimpleStorage.sol:23:9: TypeError: Member "push" not found or not visible after
argument-dependent lookup in struct SimpleStorage.People storage ref[1] storage ref.

Please advise me if there is any other suggestion.

5
  • 1
    This errors is triggered on JS side. How do you call the people getter? Also did you try using a newer Solidity version? Handling structs as return values with older solidity versions resulted in returning tuples, which depends on the js library that you use how well it is supported.
    – Richard
    Commented Dec 21, 2021 at 16:22
  • I'm currently test this code on remix. I just click the variable button on bottom left side after deploying. Also I tried to run this code on solidity 0.8.0 and 0.8.8 and get the same result. Commented Dec 21, 2021 at 19:18
  • 1
    @Richard It seems like need index number to call 'people' variable since it's array. Thank you for helping me. Commented Dec 21, 2021 at 19:30
  • sir. how did you handle this problem? I experienced the same error as you. I copy his codes and still happened again.![enter image description here](i.sstatic.net/Wt4z8.png)
    – Zhiqi Liu
    Commented Jan 6, 2022 at 3:51
  • 1
    @Zhiqi Liu, Try input index number when you click the people variable Commented Jan 6, 2022 at 16:41

2 Answers 2

3

I'm also learning from fcc haha. Not sure if you still need it but just in case if others also encounter similar problem. I don't think you're supposed to change "People[0] public people;" or "People1 public people;"

Just leave it as

People[] public people;

If I'm not wrong, you're supposed to change the people value here to 0 as it is blank by default. (the code by freecodecamp is correct)

1
  • Changing people value to 0 after deploying the code has helped to resolve the issue. thanks to @shiokempire.
    – hamza
    Commented May 12, 2022 at 11:33
1

Please pass the index value of people as 0,1 etc to retrieve the correspondent value.

Not the answer you're looking for? Browse other questions tagged or ask your own question.