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
• 26,740 points
3,063 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,370 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
• 69,220 points
2,846 views
0 votes
1 answer

Web3j v3.3.1 : Error while generating compiled solidity smart contracts which returns array of struct

Solidity does not support returning structs in ...READ MORE

answered Jun 19, 2018 in Blockchain by aryya
• 7,460 points
1,670 views
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
1,221 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
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
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,370 points
4,819 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP