Solidity invalid opcode error

0 votes

I am using a code to collect IDs from a structure and I am using the following code for it:

pragma solidity ^0.4.24;


contract MediaGallery {

address owner;

uint counter;


struct MediaAsset {

    uint id;

    string name;

    address author;

    uint createDate;

    string[] tags;

    string mediaHash;

}


mapping(address => MediaAsset[]) public mediaDatabase;


constructor () {

    owner = msg.sender;

}


function addMedia(string _name, string _mediaHash) public returns (bool success) {

    MediaAsset memory currentMedia;


    currentMedia.id = counter;

    currentMedia.name = _name;

    currentMedia.author = msg.sender;

    currentMedia.createDate = now;

    currentMedia.mediaHash = _mediaHash;


    mediaDatabase[msg.sender].push(currentMedia);


    return true;

}


function addTag(uint _id, string _tag) public returns (bool success) {

    mediaDatabase[msg.sender][_id].tags.push(_tag);


    return true;

}


function getMediaByAddress(address _user) public view returns (uint[]) {

    uint[] memory mediaAssetIds = new uint[](mediaDatabase[_user].length);


    uint numberOfMediaAssets = 0;


    for(uint i = 1; i <= mediaDatabase[_user].length;  i++) {

        mediaAssetIds[numberOfMediaAssets] = mediaDatabase[_user][i].id;

        numberOfMediaAssets++;

    }

    return mediaAssetIds;

}

}

But when I execute this code, I get invalid opcode error.

Aug 16, 2018 in Blockchain by digger
• 27,620 points
343 views

1 answer to this question.

0 votes

In the for loop, your indexing going beyond the array index. Your loop variable i has an off-by-one error. The highest value I can have is mediaDatabase[_user].length but in the loop, i is going for one value greater than this.

Modify you code to the following code:

for (uint i = 0; i < mediaDatabase[_user].length; i++) {

recompile and run, it should work.

answered Aug 16, 2018 by slayer
• 29,050 points

Related Questions In Blockchain

0 votes
1 answer

Solidity Web3js: Invalid number of arguments to Solidity function error

Try this: Change console.log(formData.destinationtoke); console.log(formData.amounttoke); var tx = sendtoken(destinationtoke, amounttoke); To : console.log(formData.destinationtoke); console.log(formData.amounttoke); var ...READ MORE

answered Oct 22, 2018 in Blockchain by Omkar
• 67,120 points
424 views
0 votes
1 answer
0 votes
1 answer

'Invalid sender' error after upgrading to geth 1.4.0

Guessing that the JSON RPC stuff changed ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
39 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
104 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Invalid opcode error in a simple Solidity contract and script

You don't need to call .at() if you're using .new(). ...READ MORE

answered Sep 14, 2018 in Blockchain by slayer
• 29,050 points
136 views
0 votes
1 answer