0

contract NumberList {
    uint256[] public bigNumberList;
}

An error is generated when attempting to call the associated getter function for bigNumberList while the array is empty. E.g bigNumberList(0) generates an error. Once I've added a number to the array however, I'm able to call the getter function without any issues.

From remix the error looks like this:


call to NumberList.bigNumberList CALL [call]from: 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4to: NumberList.bigNumberList(uint256)data: 0x35b...00000 from 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 to NumberList.bigNumberList(uint256) 0xDA0bab807633f07f013f94DD0E6A4F96F8742B53 execution cost 23744 gas (Cost only applies when called by a contract) input 0x35b...00000 decoded input { "uint256 ": "0" } decoded output { "0": "uint256: 0" } logs [] call to NumberList.bigNumberList errored: VM error: revert.

revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.


As you can see the decoded output above is actually correct but I'm currently not understanding why calling the function should produce an error?

2 Answers 2

1

You could define a function that returns the length of the array

function getBigNumberListLength() public view returns(uint){
    return bigNumberList.length
}

if you need to interact with the array, you can get the length of the array first and then based on the length, you can use a valid index

0

You cannot access an invalid index from an array.

If bigNumberList is empty then calling numberList.methods.bigNumberList(index) will fail for every index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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