9

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));
    }
}
Xaarth
  • 1,021
  • 3
  • 12
  • 34

3 Answers3

8

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

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • Can you please elaborate a bit more. Actually I just wanna know when i only create a ```constructor(with some parameters)``` then ofc the default constructor will not be created.... But the problem is what is the solution to this .... Do i need to make a constructor without arguments explicitly???? If yes then how??? – Pranshu_Taneja Mar 08 '22 at 06:33
  • 1
    @dammn_man123 You always need to pass all defined values to a function - no matter if it's a regular function or a constructor. From the code provided in your [other question](https://stackoverflow.com/q/71390659/1693192) it seems that you're not passing either `acc_id` or `_balance`. – Petr Hejda Mar 08 '22 at 08:01
  • I resolved the error. The code is successfully working now. You were right I wasn't passing values initially. Thanks for help. – Pranshu_Taneja Mar 08 '22 at 08:27
  • @dammn_man123 How do you pass values initially? – Sam Tseng Jun 05 '22 at 10:53
  • 1
    @SamTseng If you are using Remix IDE, go to the "Deploy & run transactions" tab and click the drop down arrow next to "Deploy". It should open up a tiny form where you can enter the values – Seedorf Jun 20 '22 at 03:07
  • @vajad if you have two arguments to incluede, but you forget to pass one argument, in that case also it gives this above error. – vishal Sep 04 '22 at 12:38
6

You must click on the small arrow to the right of the deploy button, then the fields will be displayed so that you can complete the data that the contract must receive. enter image description here

Ale DC
  • 1,646
  • 13
  • 20
0

enter image description here

Make sure to enter the value inside the SimpleStorage.