1

A basic smart contract with 2 arrays

// This works
uint[5] public mfb;
uint[5] public mfa;


// And this doesn't
uint[] public mfb;
uint[5] public mfa;

I'm trying to set value at index 0 inside constructor, for the arrays, but when I try to do so, contract is not deployed. I get this error :

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.

2
  • Please, include the code as text and not as an image. That makes it much easier to test and suggest changes.
    – Ismael
    Commented Aug 26, 2022 at 4:20
  • Yes I have update the question Commented Aug 26, 2022 at 9:49

2 Answers 2

1

The error saying "the constructor isn't payable" is a generic error message. Usually it is caused by something breaking inside the constructor.

If the constructor is the commented out code then the cause is that you are using mfb array before adding elements. It is initially empty, so mfb[0] causes an access out of bounds and the execution reverts.

constructor() {
    mfb.push(1000); // Add an element
    mfb[0] = 25;    // now position = 0 is valid, and can be modified
}
2
  • Yes that works, Thanks so basically, for dynamic array we need to use push first, then only we can perform operation using index. Right? Commented Aug 26, 2022 at 9:55
  • @BharatSingh You can only access a valid index. An empty array doesn't have valid indices. To grow a dynamic array you have to push elements into it.
    – Ismael
    Commented Aug 26, 2022 at 16:49
1

It seems you send ether while you deploy the contract, you can't send ether when calling a function which is not marked as payable, in other words, you can send ether only if the function is marked as payable.

constructor() payable { }

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.