1

I have got error if i try call method getArrayData()

contract ManagerDb {

    struct ManagerStruct {
        bytes32 firstname;
        bytes32 lastname;
        bytes32 email;
        uint index;
    }

    mapping ( address => ManagerStruct ) Managers;
    address[] public managerIndex; // Managers

    function insertManager(
        address managerAddress,
        bytes32 firstName,
        bytes32 lastName,
        bytes32 email
    )
        returns (bool)
    {

        ManagerStruct memory newManager;
        newManager.firstname = firstName;
        newManager.lastname = lastName;
        newManager.email = email;
        newManager.index = managerIndex.push(managerAddress)-1;

        Managers[managerAddress] = newManager;
        return true;
    }

    //for example
    function getArrayData() constant returns (bytes32[] _data1) {

        uint arrLength = managerIndex.length;
        bytes32[] memory arrData1 = new bytes32[](arrLength);
        for (uint i=0; i <= arrLength; i++){
            address addr = managerIndex[i];
            ManagerStruct ms = Managers[addr];
            arrData1[i] = ms.firstname;
        }

        return (arrData1);
    }
}

An error occurs if I substitute the index [i]. When i use constant number managerIndex[0] and here arrData1[0] it work. What am I doing wrong But if i use function getManager :

function getManager(uint index) constant returns (bytes32, address){
    ManagerStruct ms = Managers[managerIndex[index]];
    return (ms.firstname, managerIndex[index]);
}

I do not get error.

1
  • I'd check that index is valid, ie index < managerIndex.length.
    – Ismael
    Sep 1 '17 at 0:45
0

array index starts from 0 so reference to index of array.length is out of bound.

Change your for loop criteria to i < arrLength

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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