1

I work on Truffle. My contract is A and its function is infoRep(). infoRep() returns some data. Whenever i call the function in Truffle console the below error occurs:

Error: Returned error: VM Exception while processing transaction: invalid opcode    at PromiEvent (C:\Users\lenovo\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\promievent.js:9:1)

I don't know what is the solution. Please guide me to solve the problem.

A:

pragma solidity ^0.5.12;
contract A{
bytes[] info;
    function infoRep() public returns(string memory, string memory, string memory, string memory){
        info[0]="Alireza Kiakojouri";
        info[1]="500";
        info[2]="0098";
        info[3]="AUT";
           return(string(info[0]), string(info[1]), string(info[2]), string(info[3]));
    }
}
1

Change this:

bytes[] info;

To this:

bytes[4] info;

Or this:

info[0]="Alireza Kiakojouri";
info[1]="500";
info[2]="0098";
info[3]="AUT";

To this:

info.push("Alireza Kiakojouri");
info.push("500");
info.push("0098");
info.push("AUT");

But not both!

2

The problem is the array evaluating by info[<index>]. When, one creates a dynamic array, he doesn't determine its length. So, compiler can't understand how many elements that array has and doesn't create any elements for that. However, when one use info[<index>] for accessing or evaluating, compiler sees that element doesn't exist and returns an exception. Such, You should create dynamic array's elements then access them. Using .push creates the elements and after that you can access them. So, you can create and evaluate elements of bytes[] info by .push then access them by info[<index>].

So try it:

info.push("Alireza Kiakojouri");
info.push("500");
info.push("0098");
info.push("AUT");

Or try:

info= new bytes[](4);

info[0]="Alireza Kiakojouri";
info[1]="500";
info[2]="0098";
info[3]="AUT";

When one uses info= new bytes[](4); the compiler creates an 4-elements array with default values. So, you can evaluate each element immediately.

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.